Rainier
Rainier

Reputation: 11

Failed to parse the JSON document

I am getting a "Failed to parse the JSON document" error along with another error: "Caused by: groovy.json.JsonException: Lexing failed on line:" that I can't figure out.

CODE:

@Test
public void givenData() {
    RestAssured.baseURI = "https://maps.googleapis.com";

    Response returnGivenData = given().
    param("location", "-33.8670522,151.195736").
    param("radius", "500").
    param("key", "AIzaSyBde6fW-IAx1j-J5TqNOwmvx-_QPHozqRY").
    when().
    get("/maps/api/place/nearbysearch/json").
    then().assertThat().statusCode(200).and().
    contentType(ContentType.JSON).and().
    body("results[0].name", equalTo("Sydney")).and().
    body("results[0].place_id", equalTo("ChIJP3Sa8ziYEmsRUKgyFmh9AQM")).and().
    header("Server", "pablo").
    extract().
    response();
    JsonPath responseJson = ReusableMethods.convertRawDataToJson(returnGivenData);

    int count = responseJson.get("results.size()"); //ERROR LINE
    System.out.println(count);
}

ERROR:

io.restassured.path.json.exception.JsonPathException: Failed to parse the JSON document
    at io.restassured.path.json.JsonPath$ExceptionCatcher.invoke(JsonPath.java:930)
    at io.restassured.path.json.JsonPath$4.doParseWith(JsonPath.java:895)
    at io.restassured.path.json.JsonPath$JsonParser.parseWith(JsonPath.java:975)
    at io.restassured.path.json.JsonPath.get(JsonPath.java:201)
    at Demo.DemoGivenData2.givenData(DemoGivenData2.java:40)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:639)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:821)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1131)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:124)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
    at org.testng.TestRunner.privateRun(TestRunner.java:773)
    at org.testng.TestRunner.run(TestRunner.java:623)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:357)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:352)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:310)
    at org.testng.SuiteRunner.run(SuiteRunner.java:259)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1185)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1110)
    at org.testng.TestNG.run(TestNG.java:1018)
    at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:127)
Caused by: groovy.json.JsonException: Lexing failed on line: 1, column: 1, while reading 'i', no possible valid JSON value or punctuation could be recognized.
    at groovy.json.JsonLexer.nextToken(JsonLexer.java:85)
    at groovy.json.JsonLexer$nextToken.call(Unknown Source)
    at io.restassured.internal.path.json.ConfigurableJsonSlurper.parse(ConfigurableJsonSlurper.groovy:97)
    at io.restassured.internal.path.json.ConfigurableJsonSlurper$parse$0.callCurrent(Unknown Source)
    at io.restassured.internal.path.json.ConfigurableJsonSlurper.parseText(ConfigurableJsonSlurper.groovy:83)
    at io.restassured.path.json.JsonPath$4$1.method(JsonPath.java:893)
    at io.restassured.path.json.JsonPath$ExceptionCatcher.invoke(JsonPath.java:928)
    ... 27 more

Upvotes: 0

Views: 35342

Answers (8)

Ajay Varma Nandre
Ajay Varma Nandre

Reputation: 1

Just try to store it in a variable of String type and then parse that variable into the JsonPath as argument/parameter, also make sure extract the response as .asString() but not to.String().

Upvotes: 0

amar kaldate
amar kaldate

Reputation: 21

Solved for me.

In my case, the endpoint which I was passing inside get() method as an argument didn't consist of BASE_URL. I observed that Response of RestAssured.given()... expects BASE_URL + endpoint as the argument in get() method. Even baseUri() method is not required for given() in this case.

Upvotes: 0

Vrush26
Vrush26

Reputation: 1

.extract().response().asString(); instead of .toString() works for me when caught with a similar error

Upvotes: 0

Prat S
Prat S

Reputation: 1419

Using Intellij as IDE and for me "extract().asString()" worked, I had appended "toString()" earlier which was causing issue.

Upvotes: 0

mehdi ben kacem
mehdi ben kacem

Reputation: 1

Use

asString() instead of toString()

I had the same and it was fixed for me.

Upvotes: 0

nayan kumar
nayan kumar

Reputation: 1

I was having the issue and found that I was initializing the string before the line where RestAssured.baseURI = "http://...."; I fixed it.

However, in your case, I believe you can use
responseJson.getInt("results.size()")
instead of
get("results.size()"); in the error line.

Upvotes: 0

Prashant Keshari
Prashant Keshari

Reputation: 21

use .asString() methode after response(). please make sure you are using .asString() methode inplace of .tostring() and your JSONPath is correct

Upvotes: 2

shweta
shweta

Reputation: 41

I ran into same Json Exception. I found that it occurred because Json Path was not getting proper input to parse. In my case input to JsonPath was the response coming from rest assured given(), when() and then() after sending post request to a rest api and it was null. So would suggest to put logs on returnGivenData and check if it is as expected.

Upvotes: 4

Related Questions