18

Trying to find a simple way to import data from a JSON file into Python. My initial thoughts would be to read the file line by line, but this might imply some additional processing which should already be done in a library.

The ideal solution would look something like:

import json_library

the_data = json_library.load_from_file('my_file.json')

where 'my_file.json' contains a JSON-formatted variable.

1
  • 1
    2 upvotes, really? Where is the research effort? You'd expect someone with 2k rep to know how to search. This question is also not clear as it lacks an actual question. Commented Jun 20, 2014 at 18:00

1 Answer 1

23

json will do that for you.

import json
data = json.load(open('my_file.json', 'r'))

Content of demo file:

{"hello":"stack overflow"}

Demo:

>>> import json
>>> print(json.load(open('my_file.json','r')))
{u'hello': u'stack overflow'}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.