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
]
}
CodePudding user response:
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);