jsonjsonpathjson-path-expression

JSONPath: Get field starting with @ in escaped json string


I want to get a nested field in a json string using JSONPath.

Take for example the following json:

{
  "ID": "2ac464eb-352f-4e36-8b9f-950a24bb9586",
  "PAYLOAD": "{\"@type\":\"Event\",\"id\":\"baf223c4-4264-415a-8de5-61c9c709c0d2\"}"
}

If I want to extract the @type field, I expect to do it like this

$.PAYLOAD.@type

But that doesn't seem to work..

Also tried this:

$.PAYLOAD['@type']

Do I need to use escape chars or something?


Solution

  • Posting my comment as an answer


    "{\"@type\":\"Event\",\"id\":\"baf223c4-4264-415a-8de5-61c9c709c0d2\"}"
    

    Isn't JSON, it's a string containing encoded JSON.


    Since JsonPath can't decode such string, you'll have to use a language of your desire to decode the string.

    Eg: Decoding JSON String in Java