0

How to convert below nested payload into Java-JSONObject (net.minidev.json.JSONObject) for Restassured POST call request body ?

 {
  "object": "new_subway_group",
  "name": "Group 1",
  "subways": [
    1,
    2,
    3
  ]
}

1 Answer 1

1

Setup request body, use array or List for [1,2,3]

JSONObject surveyPriceObject = new JSONObject();
surveyPriceObject.put("object", "new_subway_group");
surveyPriceObject.put("name", "Group 1");
surveyPriceObject.put("subways", Arrays.asList(1,2,3));

Convert response to JSONObject

If the response has type Response then

import net.minidev.json.JSONObject;
...
JSONObject object = res.jsonPath().getObject("", JSONObject.class);
System.out.println(object);

If the response has type String then

import io.restassured.path.json.JsonPath;
import net.minidev.json.JSONObject;
...
JSONObject object = JsonPath.from(res).getObject("", JSONObject.class);
System.out.println(object);
Sign up to request clarification or add additional context in comments.

3 Comments

This is for Request body. I need to send above JSON payload inside request body. Previously I've used JSONObject surveyPriceObject = new JSONObject(); but how to use it for [1,2,3] construct?
@user2451016 I've updated my code. Sorry for misunderstood.
Thanks a lot. I have now accepted your answer as working solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.