0

If I have JSON such as

{  
   "blocks":[  
      {  
         "data":"yes",
         "_class":"yes"
      },
      {  
         "data":"no",
         "_class":"no"
      }
   ]
}

and I want to retrieve the object that only has '_class' equal to 'yes', how do I use JSONPath-plus (https://www.npmjs.com/package/jsonpath-plus) to do that?

I've been trying

  const blob = JSONPath('$.blocks[?(@._class === 'yes')]', jsonData);

but I get

ReferenceError: yes is not defined

Thanks for any help!

Cerulean
  • 5,543
  • 9
  • 59
  • 111
  • 4
    Why on earth would you do this? _You're in javascript_, the whole point of JSON is to be a string-serialised form of Javascript data for "not javascript" contexts, to be turned back into real data before you work with it, so _turn it back into normal data_ and work with that:`let obj = JSON.parse(jsonString);` and done, go work with a real object. – Mike 'Pomax' Kamermans Feb 08 '19 at 16:45
  • 1
    because you broke the string with the second quote? Try changing it to `"$.blocks[?(@._class === 'yes')]"` – AVAVT Feb 08 '19 at 16:50
  • @Mike'Pomax'Kamermans -- I'm getting a complex JSON structure from an API and want to reduce it to a simple one. I believe it's already parsed -- the point is I want to filter it to get only pieces, without having to write all the code by hand to do so. In fact I'd already done that for some sections, and a senior developer on my team suggested this library to make it easier going forward....if you have any suggestion, I'm open to them.... – Cerulean Feb 08 '19 at 16:50
  • @AVAVT -- Perfect, thanks -- I actually saw that right after I posted but that was indeed it! – Cerulean Feb 08 '19 at 16:54
  • 1
    @Cerulean you're in Javascript, how is using standard array operations when you know the object shape any more work than trying to find data inside a string, which will be _interpreting the string anyway_? `let yesClasses = obj.blocks.filter(o => o._class === 'yes')` oh look we're done. If a senior developer on your team suggests treating JSON as if it's XML _in JavaScript itself_ I have serious questions for your developer. – Mike 'Pomax' Kamermans Feb 08 '19 at 16:58
  • 1
    @Mike'Pomax'Kamermans In this case it's trivial, of course. But in real life, it's a massive API which we don't control: blocks within blocks with sub-blocks with data-arrays and fields. As I said, I did indeed do it all in JS ('by hand') for some components, and it's fine, good practice. But this is a React project and I thought of having to do it by hand for many, many components, and just wondered if there were an easier way, – Cerulean Feb 08 '19 at 17:02
  • that was also more legible for other developers coming to the code later than assembling data structures with maps, filters, object.assigns, etc. Perhaps this library won't save me time, but, as said, it was recommended. – Cerulean Feb 08 '19 at 17:03
  • And finally, there are many _different_ APIs coming from many different endpoints. While it IS good to know JS, it just seemed like days of busy work extracting data from the API using JS. But I'm learning, so I asked around, and this (along with a schema validator, which I don't know is useful) was recommended to me. As it's a Redux project, was thinking of Normalizr, but that's a different use case I presume. – Cerulean Feb 08 '19 at 17:06
  • @Mike'Pomax'Kamermans It turned out to be easier to do it with JS in the end, just using some destructuring and assignments. The only thing is that I'm not doing checking for `undefined` at every level of the tree, which was what I was hoping JSONPath would do for me. That was what was so time-consuming before. I'm going to assume that the data, if it arrives, is arriving correctly, at least for now. – Cerulean Feb 08 '19 at 18:30

1 Answers1

1

Be sure of your quotes and double-quotes when nesting strings.

"$.blocks[?(@._class === 'yes')]"

is the correct answer (thanks @AVAVT)

Cerulean
  • 5,543
  • 9
  • 59
  • 111