Galbar/JsonPath-PHP

JSON keys with spaces

samy opened this issue ยท 10 comments

samy commented

Hi !
We use your library in our project but I encounter the following problem : how to get data for an entry which has spaces in its key ?
Like this :

{
  "2020-08-05 15:59:09": {
    "added": {
      "evaluation": {
        "id": 1,

I tried many things like $.[the date] or $[the date] but didn't succeed:(

Have you any idea ?

Thanks !

HI @samy ,
You can use quotes $["the date"] and $['the date'].

You can find the formal language spec (of this implementation) in the readme.

You can also see some examples in the readme, too.
There is an example there (which I used as reference to answer you here) that shows how to use quoted strings to access values: $['store']

I invite you to check those if you have issues with syntax and, of course, feel free to post any further questions you may have ๐Ÿ˜„

samy commented

After analysis of the formal spec, I think the problem comes from the fact we use spaces AND colons in our keys, and the regex from the specs seems to not support it :(

Hi @samy could you give me an example of spaces + colon that does not work?

Hi,
I know it's a bit old, but i had a similar issue.

In my project, i have to exploit a huge feed where some index contains special characters like "@"
Jsonpath does not find or work properly because the "@" is taken as part of the Jsonpath language implementation.

eg:
$json = '[{"qwerty@id":"123"}]' [...] $obj->get("$..*[?(@.qwerty@id == 123]") => failed
As a temporary fix, i strip off all "@" from the feed, but it's a violent solution...

Any idea how we could "escape" these kind of character ?

Thanks

Hi @kruggs ,

The following expression should work for you: $..*[?(@['qwerty@id'] == '123')]

Please let me know if that solves you issue.

Hi, @Galbar
Thanks for your answer.
I tried and it's working :)

Thanks

Closing because original issue is stale and solution should be covered by the discussed approach

Hi @hvsharma63!

I see you are using parenthesis to group logical expressions in your query. You can see in the README.md how this implementation does not support parenthesis grouping and it is a known limitation. You can get around it by flattening the expression.

Your original expression is:

$.slots[?(
    (
        (
            '2035-05-16 08:00' >= @.slot_datetime_from && 
            '2035-05-16 08:00' < @.slot_datetime_to
        )
        ||
        (
            '2035-05-16 12:30' > @.slot_datetime_from && 
            '2035-05-16 12:30' <= @.slot_datetime_to
        )
    ) && 
    @.venue_id == '1' && 
    @.court_id == '1'
)]

The flattened expression would be:

$.slots[?(
    '2035-05-16 08:00' >= @.slot_datetime_from && 
    '2035-05-16 08:00' < @.slot_datetime_to && 
    @.venue_id == '1' && 
    @.court_id == '1'
    || 
    '2035-05-16 12:30' > @.slot_datetime_from && 
    '2035-05-16 12:30' <= @.slot_datetime_to && 
    @.venue_id == '1' && 
    @.court_id == '1'
)]

Hi @hvsharma63!

I see you are using parenthesis to group logical expressions in your query. You can see in the README.md how this implementation does not support parenthesis grouping and it is a known limitation. You can get around it by flattening the expression.

Your original expression is:

$.slots[?(
    (
        (
            '2035-05-16 08:00' >= @.slot_datetime_from && 
            '2035-05-16 08:00' < @.slot_datetime_to
        )
        ||
        (
            '2035-05-16 12:30' > @.slot_datetime_from && 
            '2035-05-16 12:30' <= @.slot_datetime_to
        )
    ) && 
    @.venue_id == '1' && 
    @.court_id == '1'
)]

The flattened expression would be:

$.slots[?(
    '2035-05-16 08:00' >= @.slot_datetime_from && 
    '2035-05-16 08:00' < @.slot_datetime_to && 
    @.venue_id == '1' && 
    @.court_id == '1'
    || 
    '2035-05-16 12:30' > @.slot_datetime_from && 
    '2035-05-16 12:30' <= @.slot_datetime_to && 
    @.venue_id == '1' && 
    @.court_id == '1'
)]

Hey thanks! I'll try this out