Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: read in an Array of json objects using json.loads()

Tags:

python

json

I have a file data.txt which contains a list of json objects like below:

[{"id":"1111","color":["blue"],"length":"120"},{"id":"1112","color":["red"],"length":"130"},{"id":"1112","color":["yellow"],"length":"136"}]

I tried to read it using python json.loads:

data = json.loads("data.txt")

but then I got the following errors. Did I miss anything here? Thanks a lot!

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.pyc in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    336             parse_int is None and parse_float is None and
    337             parse_constant is None and object_pairs_hook is None and not kw):
--> 338         return _default_decoder.decode(s)
    339     if cls is None:
    340         cls = JSONDecoder

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.pyc in decode(self, s, _w)
    363 
    364         """
--> 365         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    366         end = _w(s, end).end()
    367         if end != len(s):

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.pyc in raw_decode(self, s, idx)
    381             obj, end = self.scan_once(s, idx)
    382         except StopIteration:
--> 383             raise ValueError("No JSON object could be decoded")
    384         return obj, end

ValueError: No JSON object could be decoded
like image 797
Edamame Avatar asked Dec 01 '15 00:12

Edamame


People also ask

How do you load an array of JSON files in Python?

Open the file with any text editor. Add a [ at the very beginning of the file, and a ] at the very end. This will transform the data you have into an actual valid JSON array. Then use the json module to work with it.

What is JSON load ()?

load() json. load() takes a file object and returns the json object. It is used to read JSON encoded data from a file and convert it into a Python dictionary and deserialize a file itself i.e. it accepts a file object.


3 Answers

You're trying to read the string "data.txt". What you want is to open and read the file.

import json

with open('data.txt', 'r') as data_file:
    json_data = data_file.read()

data = json.loads(json_data)
like image 76
Agustín Lado Avatar answered Oct 20 '22 15:10

Agustín Lado


Try:

data = json.load(open("data.txt", 'r'))

json.loads interprets a string as JSON data, while json.load takes a file object and reads it, then interprets it as JSON.

like image 34
Brendan Molloy Avatar answered Oct 20 '22 15:10

Brendan Molloy


You need to open the file for reading and read it. To get the behavior you want:

with open('data.txt', 'r') as f:
    data = json.loads(f.read())

That should give you the json structure you want. Using with keeps you from having to close the file explicitly when you're finished.

like image 22
Adam C Avatar answered Oct 20 '22 15:10

Adam C