Search code examples
jsonjsonpath

JSONPath - filter by field that name contain '@'


I have JSON like:

{
    "media": {
        "@ref": "https://Bull_FTR.mpg",
        "track": [{
            "@type": "General",
            "VideoCount": "1"
        }, {
            "@type": "Video",
            "StreamOrder": "0"
        }, {
            "@type": "Audio",
            "StreamOrder": "1"
        },
        {
            "@type": "Audio",
            "StreamOrder": "2"
        }]
    }
}

and I need to query all fields from mwdia.tracks that have @type = 'Audio'. Problem is that field that I want to filter contains '@' and my query $.media.track[?(@.['@type'] == 'Audio')] does not work. What I am doing wrong?


Solution

  • the easy way is replace the '@' character by it's hexadecimal code (\x40) directly into your jsonpath query:

    $.media.track[?(@['\x40type']=="Audio")]