1

I am just trying JsonPath. It's great but I have a problem with a special case. I searched here but could not find a solution.

So, my JSON file is below:

 [
        {
            "id": 1,
            "images": [
                { "id": 1,"url": "http://url1.jpg" },
                { "id": 2,"url": "http://url2.jpg" }
            ]
        },
        {
            "id": 2,
            "images": [
                { "id": 1,"url": "http://url3.jpg" },
                { "id": 2,"url": "http://url4.jpg" }
            ]
        },
        {
            "id": 3,
            "images": [
                { "id": 1,"url": "http://url5.jpg" },
                { "id": 2,"url": "http://url6.jpg" }
            ]
        }
    ]

With this expression $.[?(@.id=='2')], the result is

[
  {
    "id": 2,
    "images": [
      {
        "id": 1,
        "url": "http://url3.jpg"
      },
      {
        "id": 2,
        "url": "http://url4.jpg"
      }
    ]
  },
  {
    "id": 2,
    "url": "http://url2.jpg"
  },
  {
    "id": 2,
    "url": "http://url4.jpg"
  },
  {
    "id": 2,
    "url": "http://url6.jpg"
  }
]

But I need to restrict my query result to one level without the children. The last 3 elements are not necessary. I need only the first element.

{
    "id": 2,
    "images": [
      {
        "id": 1,
        "url": "http://url3.jpg"
      },
      {
        "id": 2,
        "url": "http://url4.jpg"
      }
    ]
  }
wp78de
  • 18,207
  • 7
  • 43
  • 71
agi31
  • 13
  • 2
  • Which JSONPath implementation and language/environment, you are trying this? When I use your query on the provided JSON with Jayway's JSONPath the expected result is returned. – wp78de Oct 27 '21 at 18:11
  • thank for your feedback I use "goessner" Jsonpath on C# – agi31 Oct 28 '21 at 06:45
  • I test Jayway and like you say it's good. "Jayway JsonPath is a Java port of Stefan Goessner JsonPath implementation." So why the result isn't the same? – agi31 Oct 28 '21 at 07:41
  • Do you mean Atif Aziz's C# port of Goessner's JSONPath library (jsonpath.cs)? I think this library is a bit dated and you would be better of using a better-maintained library like (Newtonsoft's) JSON.NET lib. The reason why there are differences is, there is no such thing as a JSONPath standard and the implementations of libraries differ. Look at this comparison chart to get an idea of the differences among the vast number of implementations across various languages are: https://cburgmer.github.io/json-path-comparison/ – wp78de Oct 28 '21 at 16:14

1 Answers1

0

As hinted in my comment, there are subtle differences in the implementation of the various JSONPath libraries. The same syntax may or may not produce the same result depending on the library at hand.

Nonetheless, we can adjust your query to make it work as expected by dropping the dot after the root symbol:

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

Note: I have tested the path above using Goessner's JavaScript implementation; the C# equivalent should look like this: $[?(@.id==2)].

wp78de
  • 18,207
  • 7
  • 43
  • 71
  • My lib is JSON.NET. $[?(@.id==2)] is working. The syntax is less permissive in C# than PHP/JS and I compare them. Thank a lot for the time to explain me. – agi31 Oct 29 '21 at 07:53
  • @agi31 you are welcome. Feel free to accept and upvote my answer to your question. https://stackoverflow.com/help/someone-answers – wp78de Oct 29 '21 at 16:19