Iterate through JSON to determine if a specific object exists

Thanks for helping. I am working on python.

I request a json page and load it.

fooList = json.load(urllib.urlopen(
    "https://path.to.json.com/request?"))

It looks something like this:

{
   "data": [
      {
         "foo": "2323582"
      },
      {
         "foo": "32689023"
      },
      {
         "foo": "125815512"
      },
      {
         "foo": "1252015"
      },
      {
         "foo": "12518505"
      },
      {
         "foo": "109251907590"
      },
      {
         "foo": "2158019258"
      },
      {
         "foo": "2198059018"
      }
   ]
}

I have a specific object

obj = 1252015

Then I want to iterate over this list and present a logical answer about whether or not there objis afooList

findObj = 'This is where I need help'

Expected Result:

print findObj
True
+3
source share
1 answer
>>> print any(x['foo']=='1252015' for x in yourJson['data'])
True

anyaccepts any generator g=<generator>that returns booleans and is equivalent g[0] or g[1] or g[2] or ... or g[N]. Of course, if you do not just want to search yourJson['data'], this will be a different story, and you want to define a recursive function.

+3
source

All Articles