2

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?

2
  • There's a json module since python 2.6, use it... Commented Oct 11, 2013 at 15:19
  • 3
    you can do: import json and then json.load(open('my_file.json')) Commented Oct 11, 2013 at 15:20

1 Answer 1

5

Use the standard library :

json_data = json.loads(your_line) # Usage exemple for feature in json_data['features']: print feature['id'] 
Sign up to request clarification or add additional context in comments.

1 Comment

I was doing this but nesting another loop with for property in feature["properties"], but doing print property["api"] gave me an error "string indices must be integers". If I just use the one loop and do print feature["properties"]["api"] it works great though. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.