0

i have the below Json structure,

{
  "MyData": {
    "Type": "ABC",
    "Id": "567",
    "Number": "888"
  },
  "RefId": "9999",
  "DataValues": [
    {
      "NumberList": [
        "7777777"
      ],
      "NumberId": "888888888",
      "Phone": "0000000",
    }
  ]
}

Below is my code to parse the Json to fetch the value in NumberList,

String jsonResponse = "{  \"MyData\": {\"Type\": \"ABC\",\"Id\": \"567\",\"Number\": \"888\"},\"RefId\": \"9999\",\"DataValues\": [{\"NumberList\": [\"7777777\"],\"NumberId\": \"888888888\",\"Phone\": \"0000000\" }]}";
        List<Map<String, Object>> responseValue = com.jayway.jsonpath.JsonPath.parse(jsonResponse).read("NumberList");
        String number = responseValue.get(0).get("NumberList").toString().replace("[", "").replace("]", "");
System.out.println("number --> " + number);

The output is
number --> "7777777"

Double inverted quotes are present in the output

How to get the correct output here, need just the value 7777777

With restassured jsonPath, i get the correct output for below code.

List<Map<String, Object>> responseValue = new io.restassured.path.json.JsonPath(jsonResponse).get("NumberList");
        String number = responseValue.get(0).get("NumberList").toString().replace("[", "").replace("]", "");

But i dont want to use rest assured jsonPath.

How to get the correct output with jayway JsonPath ??

VinodKhade
  • 147
  • 1
  • 2
  • 10

1 Answers1

0

First, I think you may mean

JsonPath.parse(jsonResponse).read("DataValues")

but not

JsonPath.parse(jsonResponse).read("NumberList")

Second, it's better to use net.minidev.json.JSONArray.get() instead of toString() like this.

String jsonResponse = "{  
                           \"MyData\": {
                               \"Type\": \"ABC\",
                               \"Id\": \"567\",
                               \"Number\": \"888\"
                           },
                           \"RefId\": \"9999\",
                           \"DataValues\": [{
                               \"NumberList\": [\"7777777\"],
                               \"NumberId\": \"888888888\",
                               \"Phone\": \"0000000\" 
                           }]
                       }";
// Get "DataValues" in json
List<Map<String, Object>> responseValue = com.jayway.jsonpath.JsonPath.parse(jsonResponse).read("DataValues");
// Get "NumberList" in json. Note that "DataValues" is an array
net.minidev.json.JSONArray jsonArray = (net.minidev.json.JSONArray) responseValue.get(0).get("NumberList");
// Get the first element in "NumberList"
String number = (String) jsonArray.get(0);
System.out.println("number --> " + number);

The output is

number --> 7777777

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 14 '23 at 22:00