How do I filter JSON arrays by string using JSONPath-plus?

2.8k Views Asked by At

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!

1

There are 1 best solutions below

0
On

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

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

is the correct answer (thanks @AVAVT)