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.

Juan Carlos Coto
  • 11,900
  • 22
  • 62
  • 102
  • 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. – Tim Jun 20 '14 at 18:00

1 Answers1

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'}
timgeb
  • 76,762
  • 20
  • 123
  • 145