Is there a JSON equivalent of XQuery/XPath?

230

When searching for items in complex JSON arrays and hashes, like:

[
    { "id": 1, "name": "One", "objects": [
        { "id": 1, "name": "Response 1", "objects": [
            // etc.
        }]
    }
]

Is there some kind of query language I can used to find an item in [0].objects where id = 3?

This question is tagged with javascript json xpath xquery

~ Asked on 2011-12-12 21:50:34

The Best Answer is


125

Yup, it's called JSONPath:

It's also integrated into DOJO.

~ Answered on 2011-12-12 21:55:29


22

To summarise some of the current options for traversing/filtering JSON data, and provide some syntax examples...

  • JSPath
    .automobiles{.maker === "Honda" && .year > 2009}.model

  • json:select() (inspired more by CSS selectors)
    .automobiles .maker:val("Honda") .model

  • JSONPath (inspired more by XPath)
    $.automobiles[?(@.maker='Honda')].model

I think JSPath looks the nicest, so I'm going to try and integrate it with my AngularJS + CakePHP app.

(I originally posted this answer in another thread but thought it would be useful here, also.)

~ Answered on 2013-06-14 03:05:40


Most Viewed Questions: