Search code examples
jsonjsonpath

Filtering JSONPath with given string value


If I have a JSON like so:

{
  "data": [
    {
        "service" : { "id" : 1 }
    },
    {
        "service" : { "id" : 2 }
    },
    {
        "service" : {}
    }
  ]
}

This query works:

$..service[?(@.id==2)]

And gives expected result:

[
    {
        "id" : 2
    }
] 

However, if I had strings as id's:

{
  "data": [
    {
        "service" : { "id" : "a" }
    },
    {
        "service" : { "id" : "b" }
    },
    {
        "service" : {}
    }
  ]
}

Running similar query:

$..service[?(@.id == "a")]

Gives no results (empty array).
I am using this evaluator.
I was looking at docs here but could not find anything to point me in the right direction... Any help if someone knows how to write such query? Thanks :)


Solution

  • without " works

    $..service[?(@.id == b)]
    

    give this result

    [
       {
          "id" : "b"
       }
    ]