0

I am using JsonPath-2.4.0.jar and while trying to extract object type value from json getting different result.

String json="{\r\n  \"array\": [\r\n    1,\r\n    2,\r\n    3\r\n  ],\r\n  \"boolean\": true,\r\n  \"color\": \"gold\",\r\n  \"null\": null,\r\n  \"number\": 123,\r\n  \"object\": {\r\n    \"a\": \"b\",\r\n    \"c\": \"d\"\r\n  },\r\n  \"string\": \"Hello World\",\r\n  \"test\": {\r\n    \"test2\": {\r\n      \"test3\": \"hello\"\r\n    }\r\n  },\r\n  \"test2\": {\r\n    \"test2\": {\r\n      \"test3\": \"hello\"\r\n    }\r\n  },\r\n  \"teststring\": \"{\\\"test1\\\":\\\"abc\\\"}\"\r\n}";
        
 Object str =  JsonPath.read(json, "$.test");
 System.out.println(str);

result: {test2={test3=hello}}

why?How to get actual json object from JSON?

Thanks.

cj devin
  • 1,045
  • 3
  • 13
  • 48
  • In result instead of colon ```:``` getting equal sign and key not with double quoation mark as expected ```{ "test2": { "test3": "hello" } }``` – cj devin Dec 22 '20 at 18:24
  • This is because the object returned from the `read()` is a `LinkedHashMap` and what you are seeing is the string representation of that object (hence the `=` symbols). If you want a JSON object, do you have a specific JSON implementation you are using? – andrewJames Dec 22 '20 at 18:32
  • no,i don't have specific JSON it could be anything.given in question is just provided sample json. – cj devin Dec 22 '20 at 18:43
  • Understood - so, for example, you can use Gson and then do this: `String jsonString = new Gson().toJson(str, Map.class);` See [this question](https://stackoverflow.com/questions/22870584/convert-from-linkedhashmap-to-json-string). – andrewJames Dec 22 '20 at 18:49
  • with JsonPath any possibility to dynamically cast and mapping result. – cj devin Dec 22 '20 at 19:20
  • I don't think so, no. You need a JSON implementation such as Gson, Jackson, and so on. I may be wrong about that... You can ask in a new question! – andrewJames Dec 22 '20 at 19:27

0 Answers0