JSONPath where key or value starts with @ character?

0

I am attempting get a value based on the key containing @ symbol fails using JSONPath.
Cause: Since @ is the current object/element, json does not return the value.

Sample 1: { “firstName”: “John”, “lastName” : “doe”, “phoneNumbers”: [ { “type” : “iPhone”, “number”: “0123-4567-8888” }, { “type” : “home”, “number”: “0123-4567-8910” } ] }

This works: .phoneNumbers[1].type This works: .phoneNumbers[?(@.type==“iPhone”)].type

Sample2: { “firstName”: “John”, “lastName” : “doe”, “phoneNumbers”: [ { “**@type" : “iPhone”, “number”: “0123-4567-8888” }, { "@**type” : “home”, “number”: “0123-4567-8910” } ] }

This works: .phoneNumbers[1].type **But this does not work: .phoneNumbers[?(@.@type==“iPhone”)].type**

Any advice for dealing with keys and values containing @ characters?

I believe this expression will give you the filter you are looking for:

jsonPath($.phoneNumbers, "[?(jsonPath(@, '@**type') == 'iPhone')]")

if you’d like to collect the ‘@**type’ values then this is the expression you are looking for:

jsonPath($.phoneNumbers, "[?(jsonPath(@, '@**type') == 'iPhone')].@**type")

2 Likes

Thank you!

1 Like