Why can't Python parse this JSON data?
Your data is not valid JSON format. You have []
when you should have {}
:
[]
are for JSON arrays, which are calledlist
in Python{}
are for JSON objects, which are calleddict
in Python
Here's how your JSON file should look:
{ "maps": [ { "id": "blabla", "iscategorical": "0" }, { "id": "blabla", "iscategorical": "0" } ], "masks": { "id": "valore" }, "om_points": "value", "parameters": { "id": "valore" }}
Then you can use your code:
import jsonfrom pprint import pprintwith open('data.json') as f: data = json.load(f)pprint(data)
With data, you can now also find values like so:
data["maps"][0]["id"]data["masks"]["id"]data["om_points"]
Try those out and see if it starts to make sense.
Your data.json
should look like this:
{ "maps":[ {"id":"blabla","iscategorical":"0"}, {"id":"blabla","iscategorical":"0"} ],"masks": {"id":"valore"},"om_points":"value","parameters": {"id":"valore"}}
Your code should be:
import jsonfrom pprint import pprintwith open('data.json') as data_file: data = json.load(data_file)pprint(data)
Note that this only works in Python 2.6 and up, as it depends upon the with
-statement. In Python 2.5 use from __future__ import with_statement
, in Python <= 2.4, see Justin Peel's answer, which this answer is based upon.
You can now also access single values like this:
data["maps"][0]["id"] # will return 'blabla'data["masks"]["id"] # will return 'valore'data["om_points"] # will return 'value'
Justin Peel's answer is really helpful, but if you are using Python 3 reading JSON should be done like this:
with open('data.json', encoding='utf-8') as data_file: data = json.loads(data_file.read())
Note: use json.loads
instead of json.load
. In Python 3, json.loads
takes a string parameter. json.load
takes a file-like object parameter. data_file.read()
returns a string object.
To be honest, I don't think it's a problem to load all json data into memory in most cases.I see this in JS, Java, Kotlin, cpp, rust almost every language I use.Consider memory issue like a joke to me :)
On the other hand, I don't think you can parse json without reading all of it.