If you want to iterate over both keys and values of the dictionary, do this:
for key, value in data.items():
print(key, value)
Answer from Lior on Stack OverflowIf you want to iterate over both keys and values of the dictionary, do this:
for key, value in data.items():
print(key, value)
What error is it giving you?
If you do exactly this:
data = json.loads('{"lat":444, "lon":555}')
Then:
data['lat']
SHOULD NOT give you any error at all.
python - Reading JSON from a file - Stack Overflow
What is the difference between json.load() and ...
JSON load() vs loads()
load() loads JSON from a file or file-like object
loads() loads JSON from a given string or unicode object
It's in the documentation
More on reddit.comJson.loads only returns names but not the values
Videos
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?.
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.
Yes, s stands for string. The json.loads function does not take the file path, but the file contents as a string. Look at the documentation.
Simple example:
with open("file.json") as f:
data = json.load(f) # ok
data = json.loads(f) # not ok, f is not a string but a file
text = '{"a": 1, "b": 2}' # a string with json encoded data
data = json.loads(text)
Just going to add a simple example to what everyone has explained,
json.load()
json.load can deserialize a file itself i.e. it accepts a file object, for example,
# open a json file for reading and print content using json.load
with open("/xyz/json_data.json", "r") as content:
print(json.load(content))
will output,
{u'event': {u'id': u'5206c7e2-da67-42da-9341-6ea403c632c7', u'name': u'Sufiyan Ghori'}}
If I use json.loads to open a file instead,
# you cannot use json.loads on file object
with open("json_data.json", "r") as content:
print(json.loads(content))
I would get this error:
TypeError: expected string or buffer
json.loads()
json.loads() deserialize string.
So in order to use json.loads I will have to pass the content of the file using read() function, for example,
using content.read() with json.loads() return content of the file,
with open("json_data.json", "r") as content:
print(json.loads(content.read()))
Output,
{u'event': {u'id': u'5206c7e2-da67-42da-9341-6ea403c632c7', u'name': u'Sufiyan Ghori'}}
That's because type of content.read() is string, i.e. <type 'str'>
If I use json.load() with content.read(), I will get error,
with open("json_data.json", "r") as content:
print(json.load(content.read()))
Gives,
AttributeError: 'str' object has no attribute 'read'
So, now you know json.load deserialze file and json.loads deserialize a string.
Another example,
sys.stdin return file object, so if i do print(json.load(sys.stdin)), I will get actual json data,
cat json_data.json | ./test.py
{u'event': {u'id': u'5206c7e2-da67-42da-9341-6ea403c632c7', u'name': u'Sufiyan Ghori'}}
If I want to use json.loads(), I would do print(json.loads(sys.stdin.read())) instead.
Can someone explain what the difference is between using either load() or loads() is with the JSON library? And which, if either, is the preferred method.
I'm writing a simple script where I want the JSON data from a URL parsed out into a list. Both of these options seem to work:
import json import urllib2 url = "string to url" response = urllib2.urlopen(url) data = json.load(response)
or
import json import urllib2 url = "string to url" response = urllib2.urlopen(url) data = json.loads(response.read())
I know that there are other libraries available for parsing out JSON data, but for the time being I'm working only with the json and urllib2 libraries.
Any insight into which one should be used?
Thanks
load() loads JSON from a file or file-like object
loads() loads JSON from a given string or unicode object
It's in the documentation
The "s" is an abbreviation for "string". "dump__s__" is read as "dump string". "load__s__" = "load string". Otherwise these methods want a file-like object. This convention is scattered throughout python and even 3rd-party packages.