Guy Erez
Guy Erez

Reputation: 11

Using JsonPath to parse JSON String recursively

I'm trying to parse a given JSON in String format, for example:

{
 "id": "indeed",
 "interaction_data":
  "{\"data\":\"{\\\"something\\\":\\\"blabla\\\"}\",\"somethingElseNotNested\":\"Indeed\"}"
}

I'm working with Kotlin, and I called JsonPath.parse on the value above, the problem is, interaction_data is parsed as a String, instead of it being treated as a JSON as well.

So when I call read("$.interaction_data.data.something") it gives me an error, since interaction_data is treated as a String, instead of an object.

Any way around this? (other than parsing this part separately, I need to handle this generically).

Thanks!

Upvotes: 1

Views: 531

Answers (1)

Serge
Serge

Reputation: 43860

Json interaction_data property is triple stringifyied. Why you don't try this

var jsonObject=..your json;

 var jsonParsed=JSON.parse(jsonObject.interaction_data);
 jsonParsed.data=JSON.parse(jsonParsed.data);
JsonObject.interaction_data=jsonParsed;

result

{
"id":"indeed",
"interaction_data":{"data"{"something":"blabla"},"somethingElseNotNested":"Indeed"}
}

Upvotes: 1

Related Questions