0

I have to parse the JSON response, which has a leet speak, the node which I want to extract is the child of leet speak. am not able to extract the required child form the response. Ex: following is the JSON structure. from which I want to extract name

"debug": {
    "|\"|2()|\\|+3/\\/|)": {
      "child1": [],
      "child2": {
        "Name": "abcd", 
        "Id": "123"
      },
      "child3": {
        "location": "Delhi"
      }
    }
  }
  • in the above JSON structure "|\"|2()|\\|+3/\\/|)" is the leet speak, which is dynamic for every request it changes. – Atitkumar Shingatalur Aug 13 '19 at 07:53
  • Be more specific. What have you done so far? – Nikolai Shevchenko Aug 13 '19 at 08:06
  • @NikolayShevchenko From the above Json I want to extract Name, which is "abcd". I was extracting the name earlier, when there was no leet speak, now the developers added the leet speak recently and am not able to extract the child nodes of the leet speak. – Atitkumar Shingatalur Aug 13 '19 at 08:09
  • I tried by getting the child of "debug" as hash map and use the key and and access the name as below `debug.|\"|2()|\\|+3/\\/|).child2.name` but its througing **java.lang.IllegalArgumentException: Invalid JSON expression:** – Atitkumar Shingatalur Aug 13 '19 at 08:13

2 Answers2

0

JSON document is equivalent of the Map, and your leet speak string is a key. That is very bad idea as keys should be easily identifiable. So I would suggest to re-think the structure. May be introduce another level where your leet speak string would be contained as a child under some easily identifiable key. But if you can not do that, you can convert your JSON into Map and then extract ALL keys and then traverse through them and find the one that you need and then get your content by that key

Michael Gantman
  • 7,315
  • 2
  • 19
  • 36
  • Hi @MichaelGantman am a **Test Automation Engineer**, and automating API's using rest-assured. Here I can't do anything with the structure, this is how the response will be and I have to validate the name on the JSON response, due to this Leet Speak am not able to traverse till name. when I tried accessing this by – Atitkumar Shingatalur Aug 13 '19 at 09:14
  • when I tried accessing this by `debug.|\"|2()|\\|+3/\\/|).child2.name` but its througing **java.lang.IllegalArgumentException: Invalid JSON expression:** at ``debug.|`` – Atitkumar Shingatalur Aug 13 '19 at 09:19
  • The code debug.|\"|2()|\\|+3/\\/|).child2.name appears to be some propritory library code that works with JSON directly. I suggested to first convert it to a Map instance and to work with the map. Either way, my suggestion is to do it in 2 steps: first extract just the top level keys and traverse through the keys only to find the one you need. Then use the found value as key to find the object of your interest in the map (Or in JSON if you work with some library parsing JSON directly) – Michael Gantman Aug 13 '19 at 09:44
0

You can create pojo and get that node value

Gson g = new Gson(); Pojo1 p1=g.fromJson(jo.toString(), Pojo1.class);

    System.out.println(p1.getDebug().get23().getChild2().getName());
Anubhav
  • 23
  • 1
  • 3