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

前端 未结 8 1991
误落风尘
误落风尘 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: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.

提交回复
热议问题