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

前端 未结 8 1918
误落风尘
误落风尘 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 09:12

    Try to using JSPath -- https://github.com/dfilatov/jspath.

    JSPath is a domain-specific language (DSL) that enables you to navigate and find data within your JSON documents. Using JSPath, you can select items of JSON in order to retrieve the data they contain.

    JSPath for JSON like an XPath for XML.

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

    Not exactly what you are looking for, but check out object-scan. It is a bit more verbose, but is much more powerful and supports more (complex) use cases. Here is an example of how one could use it

    const objectScan = require('object-scan');
    
    const Characters = [{"id":"CuriousGeorge","species":"Monkey","mood":"curious","appendage":[{"type":"hand","side":"left","holding":[{"id":"Banana"}]},{"type":"hand","side":"right","holding":[]},{"type":"foot","side":"left","holding":[]},{"type":"foot","side":"right","holding":[{"id":"YellowHat"},{"id":"Keys"}]}]},{"id":"ManInYellowHat","species":"Human","mood":"angry"}];
    
    console.log(objectScan(['**.holding.id'], {
      useArraySelector: false,
      abort: true,
      rtn: 'parent',
      filterFn: ({ value }) => value === 'Banana'
    })(Characters));
    // => { id: 'Banana' }
    
    console.log(objectScan(['**.appendage[*]'], {
      rtn: 'value',
      filterFn: ({ value }) => value.type === 'hand' && value.side === 'left'
    })(Characters));
    // => [ { type: 'hand', side: 'left', holding: [ [Object] ] } ]
    

    Make sure you check out the readme. It has a lot of examples in it.

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