Ben
Ben

Reputation: 345

Python: Parsing a single-line JSON file

I'm trying to parse a large one-line JSON file and can't seem to figure it out. I've looked for resources on here and elsewhere, but most of what I see tells you to parse everything line-by-line. Since I'm working with one really long line, what's the best way to parse this with python?

Specifically, I'm looking for one particular value that's in a nested dictionary in the JSON data. The data looks like this (after running it through a formatter):

{
   "type":"FeatureCollection",
   "features":[
      {
         "type":"Feature",
         "id":"wells.529038",
         "geometry":null,
         "properties":{
            "api":"4245180382"
         }
      },
      {
         "type":"Feature",
         "id":"wells.481699",
         "geometry":null,
         "properties":{
            "api":"4237182573"
         }
      }
   ]
}

I want to extract all the api values, but I'm having trouble wrapping my head around how to do that given the multi-nested structure and because the file is huge and only one line. What the best approach here?

Upvotes: 2

Views: 6394

Answers (1)

Co_42
Co_42

Reputation: 1269

Use the standard library :

json_data = json.loads(your_line)

# Usage exemple
for feature in json_data['features']:
    print feature['id']

Upvotes: 5

Related Questions