Why can't Python parse this JSON data?

ID : 745

viewed : 137

Tags : pythonjsonparsingpython

Top 5 Answer for Why can't Python parse this JSON data?

vote vote

99

Your data is not valid JSON format. You have [] when you should have {}:

  • [] are for JSON arrays, which are called list in Python
  • {} are for JSON objects, which are called dict 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 json from pprint import pprint  with 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.

vote vote

88

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 json from pprint import pprint  with 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' 
vote vote

74

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.

vote vote

68

data = [] with codecs.open('d:\output.txt','rU','utf-8') as f:     for line in f:        data.append(json.loads(line)) 
vote vote

57

"Ultra JSON" or simply "ujson" can handle having [] in your JSON file input. If you're reading a JSON input file into your program as a list of JSON elements; such as, [{[{}]}, {}, [], etc...] ujson can handle any arbitrary order of lists of dictionaries, dictionaries of lists.

You can find ujson in the Python package index and the API is almost identical to Python's built-in json library.

ujson is also much faster if you're loading larger JSON files. You can see the performance details in comparison to other Python JSON libraries in the same link provided.

Top 3 video Explaining Why can't Python parse this JSON data?

Related QUESTION?