Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestAssured JsonPath to String

Is there a way to convert the whole JSON path data to a string in Java?

I am working on APIs and their responses are in JSON format. It is easy to understand the JSON structure through Postman/WireShark but I am trying to run an API request through Java, grab the response, convert the raw response to JSON, convert JSON response to a string format and print it. The method '.getString()' is to access a particular element and not the whole data. The method '.toString()' does not work on JSON data either.

    JsonPath json = (ConvertRawFiles.rawtoJSON(response));
    String id   = json.get("id");
    log.info("The id is " + id);

    JsonPath json = (ConvertRawFiles.rawtoJSON(response));
    String complete_json_data   = ???;
    log.info("The complete json data is " + complete_json_data);

The code snippet which is mentioned "???" is what I was trying to achieve.

like image 643
Satya Avatar asked Jan 17 '26 09:01

Satya


1 Answers

The methods which can convert a JsonPath object into a String are:

JsonPath json = (ConvertRawFiles.rawtoJSON(response));
String complete_json_data = json.prettify();

and

JsonPath json = (ConvertRawFiles.rawtoJSON(response));
String complete_json_data = json.prettyPrint();
like image 111
Satya Avatar answered Jan 19 '26 23:01

Satya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!