JSONPath or other XPath like utility for JSON/Javascript; or Jquery JSON

前端 未结 8 1917
误落风尘
误落风尘 2020-12-28 08:39

I have been looking at JSONPath and though it seems pretty well done, I wonder if anyone has worked with it and can comment on its usability, or can recommend alternatives?

相关标签:
8条回答
  • 2020-12-28 08:47

    OK, I have created a prototype for this, available here: http://code.google.com/p/jfunk/

    It has already proven useful for me, so I will probably slowly improve and refactor it into something nice. But if I get good feedback, I can move more quickly. I would also welcome help.

    0 讨论(0)
  • 2020-12-28 08:53

    I just wrote a clientside JS-lib that does just this - it makes it possible to query a JSON structure with XPath.

    @jlarson - with "defiant.js", you can query your JSON structure like this (this lib extends the global JSON object):

    JSON.search( Characters, '//*[id="Banana"]' );
    



    This call will return an array with matching nodes AND those matches won't be detached from your original JSON object (same behaviour as when working with XML + XPath). To illustrate what I mean, here is a little pseudo(-ish) code:

    var store = {
        "book": [
            {
                "id": 1,
                "price": 8.95,
                "title": "Sayings of the Century",
                "category": "reference",
                "author": "Nigel Rees"
            },
            {
                "id": 2,
                "price": 22.99,
                "title": "The Lord of the Rings",
                "category": "fiction",
                "author": "J. R. R. Tolkien",
                "isbn": "0-395-19395-8"
             }
        ]
    };
    
    var b1 = JSON.search( store, '//book[1]' );
    b1[0].isbn = '12345';
    
    console.log( store.book[0].isbn );
    // 12345
    

    This lib is thus far for browser and clientside but I've plans to re-write it for NodeJS eventually. Check out the Xpath evaluator here; that demonstrates the functionality. There are also pre-written Xpath expressions as well:

    http://defiantjs.com/#xpath_evaluator

    You can find the lib at Github:
    https://github.com/hbi99/defiant.js

    Finally, there is a little more functionality in "defiant.js" and if you're interested, you'll hopefully read about it over there (http://defiant.com)

    I hope you'll find it useful.

    0 讨论(0)
  • 2020-12-28 08:56

    Dojo's dojo.getObject has a facility that works loosely like this, where you can provide a path like "a.b.c" to the property you want to fetch.

    Check it out:

    http://api.dojotoolkit.org/jsdoc/1.3/dojo.getObject

    I don't think it understands arrays quite that well and I think it is missing a full-featured selector language like the one you are suggesting.

    As for usage, I've coded a selector language like the one you are suggesting, but for a client, and array addressing is very proprietary to their particular object structure.

    I would definitely use a system like this if you were to make it, and perhaps even contribute if I saw an area I could help with.

    0 讨论(0)
  • 2020-12-28 09:00

    Check out JSON Select - CSS-like selectors for JSON.

    0 讨论(0)
  • 2020-12-28 09:06

    Looks like there's a new option: jQuery-JSONPath. Seems to be exactly what you're asking for.

    0 讨论(0)
  • 2020-12-28 09:07

    Definitely would be a useful utility.

    My opinion is that the best way to approach this would be to stay as similar as possible to css selectors, as you indicate. I'd recommend looking under the hood at jquery's implementation of selectors.

    I would suggest something like

    var banana = object.function(jsonObect, "holding #Banana");
    var leftHands = object.function(jsonObject, "appendage[type=hand][side=left]");
    

    instead of your usage examples.

    I'm not sure how the upcoming native json support will affect this...

    0 讨论(0)
提交回复
热议问题