Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dot key + filter #971

Open
Etwenn opened this issue Nov 30, 2023 · 1 comment
Open

Dot key + filter #971

Etwenn opened this issue Nov 30, 2023 · 1 comment

Comments

@Etwenn
Copy link

Etwenn commented Nov 30, 2023

Hello, i'm using JsonPath + SPEL
Considering this json :

{
    "item.thing": [
        {"key" : "nomatch", "value" : "a"},{"key" : "match", "value" : "b"},{"key" : "nomatch", "value" : "c"}
    ]
}

i'm trying to reach item.thing and filter the array like this :

{item.thing[?(@.key=="match")].value}.get(0)=='b'"
it's doesn't works because "item" key is missing from my json (seems fair)

so i tried to escape the key :
{"item.thing"[?(@.key=="match")].value}.get(0)=='b'"
but doesn't seems to work neither apparently due to the filter function

not sure how to find correct syntax neither if it's even possible with filter function

it's working when i don't have a key with a dot, e.g :
{item[?(@.key=="match")].value}.get(0)=='b'"

any suggestion ?

@Rudraksh2003
Copy link

The problem arises because dots are typically used as delimiters in JSONPath expressions, so having a key with a dot in it can lead to unexpected behavior.

In this case, since your key is "item.thing," it is being interpreted as a nested structure. You've tried escaping the dot with quotes, but it seems that the filter function is causing complications.

One way to approach this is to use the bracket notation to access the property. Here's an example that might work for your case:

json

['item.thing'][?(@.key=="match")].value
This way, you are treating "item.thing" as a single key rather than a nested structure. The bracket notation can help in cases where the key contains special characters like dots.

Your expression might look like this:

json

['item.thing'][?(@.key=="match")].value.get(0)=='b'

This assumes that the JSONPath library you are using supports the bracket notation for keys.

If this doesn't work, you might need to refer to the documentation or specific implementation details of the JSONPath library you are using to see if there are any special considerations or syntax for handling keys with dots. If the library supports escaping characters in keys, check for the correct syntax according to its documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants