JSON has become an ubiquitous data exchange format everywhere. Pretty much every service has a JSON API. And since it is so popular, most of the programming languages have built-in JSON parsers. And Of course, Python is no exception. In this article, I'll show you how you can parse JSON with Python's json
library.
JSON parsing in Python is quite straight forward and easy unlike in some languages, where it is unnecessarily cumbersome. Like everything else in Python, You start by importing the library you want.
In this article, I am going to use the following JSON I got from json.org
{
"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}
}
We have got a good set of dictionaries and arrays to work with in this data. If you want to follow along, you can use the same JSON or you can use anything else as well.
The first thing to do is to get this json string into a variable.
json_string = """{"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}}"""
And now we parse this string into a dictionary
object with the help of the json
library's loads()
method.
json_dict = json.loads(json_string)
And you're done. The JSON is parsed and is stored in the json_dict
object. The json_dict
here is a python dictionary object. If you want to verify, you can do that by calling the type()
on it with
And it will show that it is <class 'dict'>
.
Getting back, We have the entire json object as a dictionary in json_dict
object and you can just drill down into the dictionary with the keys. On the top level, We just have one key in the dictionary which is menu
. We get can get that by indexing the dictionary with that key.
And of course menu
is a dictionary too with the keys id
, value
, and popup
. We can access them and print them as well.
print(menu['id']) ## => 'file'
print(menu['value']) ## => 'File'
And then finally we've got popup
which is another dictionary as well with the key menuitem
which is a list. We can verify this by checking the types of these objects.
popup = menu['popup']
print(type(popup)) ## => <class 'dict'>
menuitem = popup['menuitem']
print(type(menuitem)) ## => <class 'list'>
And Since menuitem
is a list, we can iterate on it and print the values.
for item in menuitem:
print(item)
And the output is
{'value': 'New', 'onclick': 'CreateNewDoc()'}
{'value': 'Open', 'onclick': 'OpenDoc()'}
{'value': 'Close', 'onclick': 'CloseDoc()'}
And of course each of these elements are dictionaries and so you can go further inside and access those keys and values.
For example, If you want to access New
from the above output, you can do this:
print(menuitem[0]['value']) ## => New
And so on and so forth to get any value in the JSON.
And not only that, json
library can also accept JSON responses from web services. One cool thing here is that, web server responses are byte
strings which means that if you want to use them in your program you'd have convert them to regular strings by using the decode()
method. But for json
you don't have to do that. You can directly feed in the byte
string and it will give you a parsed object. That's pretty cool!
That is all for this article.
For more programming articles, checkout Freblogg Freblogg/Python
Some of my other articles on automation:
Web Scraping For Beginners with Python
My semi automated workflow for blogging
Publish articles to Blogger automatically
Publish articles to Medium automatically
This is the 19th article as part of my twitter challenge #30DaysOfBlogging. Eleven more articles on various topics, including but not limited to, Java, Git, Vim, Python, to come.
If you are interested in this, make sure to follow me on Twitter @durgaswaroop. While you're at it, Go ahead and subscribe on medium and my blog as well.
If you are interested in contributing to any open source projects and haven't found the right project or if you were unsure on how to begin, I would like to suggest my own project, Delorean which is a Distributed Version control system, built from scratch in scala. You can contribute not only in the form of code, but also with usage documentation and also by identifying any bugs in its functionality.
Thanks for reading. See you again in the next article.