Reading JSON from a file

Question:

A simple looking, easy statement is throwing some errors in my face.

I have a JSON file called strings.json like this:

"strings": [{"-name": "city", "#text": "City"}, {"-name": "phone", "#text": "Phone"}, ...,
            {"-name": "address", "#text": "Address"}]

I want to read the JSON file, just that for now. I have these statements which I found out, but it’s not working:

import json
from pprint import pprint

with open('strings.json') as json_data:
    d = json.loads(json_data)
    json_data.close()
    pprint(d)

The error displayed on the console was this:

Traceback (most recent call last):
  File "/home/.../android/values/manipulate_json.py", line 5, in <module>
    d = json.loads(json_data)
  File "/usr/lib/python2.7/json/__init__.py", line 326, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: expected string or buffer
[Finished in 0.1s with exit code 1]

If I use json.load instead of json.loads, I get this error:

Traceback (most recent call last):
  File "/home/.../android/values/manipulate_json.py", line 5, in <module>
    d = json.load(json_data)
  File "/usr/lib/python2.7/json/__init__.py", line 278, in load
    **kw)
  File "/usr/lib/python2.7/json/__init__.py", line 326, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 369, in decode
    raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 829 column 1 - line 829 column 2 (char 18476 - 18477)
[Finished in 0.1s with exit code 1]
Asked By: R.R.C.

||

Answers:

The json.load() method (without "s" in "load") can read a file directly:

import json

with open('strings.json') as f:
    d = json.load(f)
    print(d)

You were using the json.loads() method, which is used for string arguments only.


The error you get with json.loads is a totally different problem. In that case, there is some invalid JSON content in that file. For that, I would recommend running the file through a JSON validator.

There are also solutions for fixing JSON like for example How do I automatically fix an invalid JSON string?.

Answered By: ubomb

Here is a copy of code which works fine for me,

import json

with open("test.json") as json_file:
    json_data = json.load(json_file)
    print(json_data)

with the data

{
    "a": [1,3,"asdf",true],
    "b": {
        "Hello": "world"
    }
}

You may want to wrap your json.load line with a try catch, because invalid JSON will cause a stacktrace error message.

Answered By: user1876508

The problem is using the with statement:

with open('strings.json') as json_data:
    d = json.load(json_data)
    pprint(d)

The file is going to be implicitly closed already. There is no need to call json_data.close() again.

Answered By: Zongjun

In Python 3, we can use the below method.

Read from a file and convert to JSON

import json
from pprint import pprint

# Considering "json_list.json" is a JSON file

with open('json_list.json') as fd:
     json_data = json.load(fd)
     pprint(json_data)

The with statement automatically closes the opened file descriptor.


String to JSON

import json
from pprint import pprint

json_data = json.loads('{"name" : "myName", "age":24}')
pprint(json_data)
Answered By: Thejesh PR

To add on this, today you are able to use pandas to
import JSON: pandas.read_json

You may want to do a careful use of the orient parameter.

Answered By: Ando Jurai

You can use the Pandas library to read the JSON file.

import pandas as pd
df = pd.read_json('strings.json', lines=True)
print(df)
Answered By: drorhun
def read_JSON():
    with open("FILE PATH", "r") as i:
        JSON_data = i.read()
    print(JSON_data)
Answered By: Pushparaj
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.