1

Failed to parse the JSON document: why I am getting this error. I am trying to pass argument to ajson body to perform a delete operation. The data is stored in an array using TestNg notations. When I run the test, it fails with message "failed to parse the Json Document"

import org.testng.annotations.DataProvider;
    import org.testng.annotations.Test;

import Files.BasicsPayloadBody;
import io.restassured.RestAssured;
import io.restassured.path.json.JsonPath;

import static io.restassured.RestAssured.*;
public class Deletebook {
    
    @Test(dataProvider="deletedata")
    public static void deletebooks(String deletebookid)
    {
        RestAssured.baseURI="http://216.10.245.166";
        String DeleteResponse = given().header("Content Type","application/json")
                               .body(BasicsPayloadBody.deletebookdata(deletebookid))
                               .when().post("/Library/DeleteBook.php")
                               .then().extract().response().asString();
            
            JsonPath js = new JsonPath(DeleteResponse);
            System.out.println(js.getString("msg"));
    }

    @DataProvider(name="deletedata")
    public Object[] getdata1()
    {
        return new Object[]{"testeqweedh47ee76","testdweeed145eeddd65","teseetsdd2teseet3wdd"};
    }

    
}



Json Body:

public static String deletebookdata(String deletebookid)
    {
        String deleteid = "{\r\n" + 
                " \r\n" + 
                "\"ID\" : \""+deletebookid+"\r\n" + 
                " \r\n" + 
                "} \r\n" + 
                "";
        return deleteid;
    }

1 Answers1

0

The closing quote in the JSON is missing:

String deleteid = "{\r\n" + 
            " \r\n" + 
            "\"ID\" : \""+deletebookid+"\"\r\n" + // <-- Added backslash and quote here
            " \r\n" + 
            "} \r\n" + 
            "";

BTW you don't need all those line breaks and spaces inside the JSON:

String deleteid = "{\"ID\":\"" + deletebookid + "\"}";

This is much easier to read.

RoToRa
  • 37,635
  • 12
  • 69
  • 105