Select json object based on key value pair

FlavorScape picture FlavorScape · Mar 6, 2012 · Viewed 7.2k times · Source

This might be a noob question, but I'm wondering if there's a way to select a json object without having to loop through key value pairs. I've done similar things with e4x, but I'm not sure how to do it syntactically for js. For example

 var data =   { "objects":[
                {"foo":"x","bar":"a"},
                {"foo":"y","bar":"b"}
              ]}

So instead of a for loop, some way to declare

 var someObject = data.objects[where objects.foo == x]

Answer

kirilloid picture kirilloid · Mar 6, 2012

You may do that w/o manually iterate over data, but some code should iterate over object anyway (so doesn't expect lightning speed on rather large objects).

There's a library for that: jsonpath