woddle woddle - 6 months ago 38
JSON Question

Apply JSONPath filter to field with space

I'm trying to use JSONPath (https://github.com/jayway/JsonPath) to search a document which has spaces in the field names:

{
"model": {
"Details": {
"Nospace": "New today",
"Random nonsense": "New today"
}
}
}


I'm testing using the evaluator at http://jsonpath.herokuapp.com/

This works:

$.model.Details[?(@.Nospace== 'New today')]


But this does not:

$.model.Details[?(@.'Random nonsense'== 'New today')]


This does but is missing the filter expression:

$.model.Details['Random nonsense']


So it seems it's possible to refer to fields with spaces, but I haven't found how to use them in a filter. Is it possible? I have tried many other combinations with no luck, and don't seem to find anything online about it either.

Thanks.

Answer

Extra brackets.

$.model.Details[?(@['Random nonsense'] == 'New today')]