0

I have json string i want to direclty access element like childMap4.childMap3.ch3Name and my json string is i am not able find proper solutions.

{
  "is working": true,
  "childMap4": {
    "childMap3": {
      "childMap2": {
        "childMap1": {
          "ch1Name": "childMap1"
        },
        "ch2Name": "childMap2"
      },
      "ch3Name": "childMap3 testing"
    },
    "ch4Name": "childMap4"
  },
  "prName": "parentMap",
  "salary": 800.23,
  "prName2": "parentMap field 2",
  "age": 24
}
sajeeth
  • 47
  • 2
  • 9
  • 1
    you can decode it to a Map: https://stackoverflow.com/questions/443499/convert-json-to-map A better solution would be to decode it to a custom object, but that would require more work. Depends what you need to do – Bentaye Aug 05 '22 at 09:14

2 Answers2

0

If you just want to extract the value, you should take a look at JsonPath. Just use the method JsonPath.read([json_object], [expression]) with expression $.childMap4.childMap3.ch3Name.

Another approach would be to create proper mapping class and deserialize JSON string with a library like Jackson, but if you want just single field value taht would be an overkill.

Kryszak
  • 21
  • 5
0

JsonNode jsonNode = mapper.convertValue(Obj, JsonNode.class);

jsonNode.path("childMap4").path("childMap3").path("ch3Name").toString();

  • What library provides these types? What even is the type of `mapper`? (I know the answers, but OP doesn't). What is `Obj` (and why is it capitalized), and is OP supposed to get one when all they have is a JSON string? – Jelaby Aug 05 '22 at 10:59