How to Work With JSON Data Using Python

This tutorial shows how easy it is to use the Python programming language to work with JSON data.

Before I begin the topic, let’s define briefly what we mean by JSON. Let’s see how JSON’s main website defines it:

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition – December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.

Thus, JSON is a simple way to create and store data structures within JavaScript. The reason you see JavaScript in the acronym is that a JavaScript object is created when storing data with JSON. But don’t worry, you don’t need to know JavaScript to work with JSON files—rather it is about the JSON syntax (format) itself.

In brief, JSON is a way by which we store and exchange data, which is accomplished through its syntax and is used in many web applications. The nice thing about JSON is that it has a human-readable format, and this may be one of the reasons for using it in data transmission, in addition to its effectiveness when working with APIs.

An example of JSON-formatted data is as follows:

In this tutorial, I will show you how to use Python to work with JSON files. So let’s get started!

Python and JSON

Python makes it simple to work with JSON files. The module used for this purpose is the json module. This module should be included (built-in) within your Python installation, and you thus don’t need to install any external modules as we did when working with PDF and Excel files, for instance. The only thing you need in order to use this module is to import it:

import json

But what does the json library do? This library mainly parses JSON from files or strings. It also parses JSON into a dictionary or list in Python and vice versa, that is converting a Python dictionary or list into JSON strings.

JSON to Python

Reading JSON means converting JSON into a Python value (object). As mentioned above, the json library parses JSON into a dictionary or list in Python. In order to do that, we use the loads() function (load from a string), as follows:

If you want to see the output, do a print jsonToPython, in which case you will get the following output:

{u'age': 39, u'name': u'Frank'}

That is, the data is returned as a Python dictionary (JSON object data structure). So will the statement print jsonToPython['name'] return any output? Go ahead and try it out.

As we just saw, objects in JSON are converted to dictionaries in Python. The conversion of JSON data to Python is based on the following conversion table.

JSON Python
object dict
array list
string str
number (int) int
number (real) float
true True
false False
null None

Python to JSON

In the previous section, we saw how to convert JSON into a Python value (i.e. Dictionary). In this section, I will show you how we can convert (encode) a Python value to JSON.

Say that we have the following Dictionary in Python:

If we print dictionaryToJson, we get the following JSON data:

{"age": 44, "isEmployed": true, "name": "Bob"}

So this output is considered the data representation of the object (Dictionary). The method dumps() was the key to such an operation. The conversion of Python objects to JSON data is based on the following conversion table.

Python JSON
dict object
list array
str string
int number (int)
float number (real)
False false
True true
None null

The keys in a Python dictionary have to be converted to a string for them to be used as JSON data. However, a simple string conversion is only feasible for basic types like str, int, float, and bool. For other types of keys, this can result in a TypeError. You can avoid that from happening by setting the value of skipkeys argument to True. This will tell Python to skip all keys which cannot be converted to a string.

There is another argument called sort_keys which can be set to True in order to output the dictionary after sorting it by its keys.

Back and Forth Conversion of Data

You probably already know that keys for dictionaries in Python can be of different data types like strings, int, or tuples. However, the keys in JSON data can only be strings. This means that when you convert a dictionary into JSON, all its keys will be cast to strings. Conversion of this JSON back to a dictionary will not get you back the original data type of the keys.

Storing Different Data Types or Objects as JSON

It is important to note at this point that JSON cannot store all types of Python objects, but only the following types: Lists, Dictionaries, Booleans, Numbers, Character strings, and None. Thus, any other types need to be converted in order to be stored in JSON.

Let’s say we have the following class:

Let’s say we created a new object abder, as follows:

abder = Employee('Abder')

What if we wanted to convert this object to JSON? That is json.dumps(abder)? In this case, you would get an error similar to the following:

But is there a workaround? Fortunately there is. I like the workaround described on the Python Tips website. To solve this issue, we can define a method similar to the following:

Then encode the object into JSON as follows:

jsonAbder = json.dumps(abder, default=jsonDefault)

If you print jsonAbder, you should get the following output:

{"name": "Abder"}

We have now encoded a Python object (abder) into JSON.

Conclusion

From this tutorial, we can notice that Python again and again is proving not only its ability to work with different applications, but also its flexibility to work with different issues while working with an application, as we saw in the last part of the tutorial.

If you want to know more about the json module, you can visit the documentation page.

This post has been updated with contributions from Monty Shokeen. Monty is a full-stack developer who also loves to write tutorials and to learn about new JavaScript libraries.


This content originally appeared on Envato Tuts+ Tutorials and was authored by Abder-Rahman Ali

This tutorial shows how easy it is to use the Python programming language to work with JSON data.

Before I begin the topic, let's define briefly what we mean by JSON. Let's see how JSON's main website defines it:

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.

Thus, JSON is a simple way to create and store data structures within JavaScript. The reason you see JavaScript in the acronym is that a JavaScript object is created when storing data with JSON. But don't worry, you don't need to know JavaScript to work with JSON files—rather it is about the JSON syntax (format) itself.

In brief, JSON is a way by which we store and exchange data, which is accomplished through its syntax and is used in many web applications. The nice thing about JSON is that it has a human-readable format, and this may be one of the reasons for using it in data transmission, in addition to its effectiveness when working with APIs.

An example of JSON-formatted data is as follows:

In this tutorial, I will show you how to use Python to work with JSON files. So let's get started!

Python and JSON

Python makes it simple to work with JSON files. The module used for this purpose is the json module. This module should be included (built-in) within your Python installation, and you thus don't need to install any external modules as we did when working with PDF and Excel files, for instance. The only thing you need in order to use this module is to import it:

import json

But what does the json library do? This library mainly parses JSON from files or strings. It also parses JSON into a dictionary or list in Python and vice versa, that is converting a Python dictionary or list into JSON strings.

JSON to Python

Reading JSON means converting JSON into a Python value (object). As mentioned above, the json library parses JSON into a dictionary or list in Python. In order to do that, we use the loads() function (load from a string), as follows:

If you want to see the output, do a print jsonToPython, in which case you will get the following output:

{u'age': 39, u'name': u'Frank'}

That is, the data is returned as a Python dictionary (JSON object data structure). So will the statement print jsonToPython['name'] return any output? Go ahead and try it out.

As we just saw, objects in JSON are converted to dictionaries in Python. The conversion of JSON data to Python is based on the following conversion table.

JSON Python
object dict
array list
string str
number (int) int
number (real) float
true True
false False
null None

Python to JSON

In the previous section, we saw how to convert JSON into a Python value (i.e. Dictionary). In this section, I will show you how we can convert (encode) a Python value to JSON.

Say that we have the following Dictionary in Python:

If we print dictionaryToJson, we get the following JSON data:

{"age": 44, "isEmployed": true, "name": "Bob"}

So this output is considered the data representation of the object (Dictionary). The method dumps() was the key to such an operation. The conversion of Python objects to JSON data is based on the following conversion table.

Python JSON
dict object
list array
str string
int number (int)
float number (real)
False false
True true
None null

The keys in a Python dictionary have to be converted to a string for them to be used as JSON data. However, a simple string conversion is only feasible for basic types like str, int, float, and bool. For other types of keys, this can result in a TypeError. You can avoid that from happening by setting the value of skipkeys argument to True. This will tell Python to skip all keys which cannot be converted to a string.

There is another argument called sort_keys which can be set to True in order to output the dictionary after sorting it by its keys.

Back and Forth Conversion of Data

You probably already know that keys for dictionaries in Python can be of different data types like strings, int, or tuples. However, the keys in JSON data can only be strings. This means that when you convert a dictionary into JSON, all its keys will be cast to strings. Conversion of this JSON back to a dictionary will not get you back the original data type of the keys.

Storing Different Data Types or Objects as JSON

It is important to note at this point that JSON cannot store all types of Python objects, but only the following types: Lists, Dictionaries, Booleans, Numbers, Character strings, and None. Thus, any other types need to be converted in order to be stored in JSON.

Let's say we have the following class:

Let's say we created a new object abder, as follows:

abder = Employee('Abder')

What if we wanted to convert this object to JSON? That is json.dumps(abder)? In this case, you would get an error similar to the following:

But is there a workaround? Fortunately there is. I like the workaround described on the Python Tips website. To solve this issue, we can define a method similar to the following:

Then encode the object into JSON as follows:

jsonAbder = json.dumps(abder, default=jsonDefault)

If you print jsonAbder, you should get the following output:

{"name": "Abder"}

We have now encoded a Python object (abder) into JSON.

Conclusion

From this tutorial, we can notice that Python again and again is proving not only its ability to work with different applications, but also its flexibility to work with different issues while working with an application, as we saw in the last part of the tutorial.

If you want to know more about the json module, you can visit the documentation page.

This post has been updated with contributions from Monty Shokeen. Monty is a full-stack developer who also loves to write tutorials and to learn about new JavaScript libraries.


This content originally appeared on Envato Tuts+ Tutorials and was authored by Abder-Rahman Ali


Print Share Comment Cite Upload Translate Updates
APA

Abder-Rahman Ali | Sciencx (2016-01-20T10:33:31+00:00) How to Work With JSON Data Using Python. Retrieved from https://www.scien.cx/2016/01/20/how-to-work-with-json-data-using-python/

MLA
" » How to Work With JSON Data Using Python." Abder-Rahman Ali | Sciencx - Wednesday January 20, 2016, https://www.scien.cx/2016/01/20/how-to-work-with-json-data-using-python/
HARVARD
Abder-Rahman Ali | Sciencx Wednesday January 20, 2016 » How to Work With JSON Data Using Python., viewed ,<https://www.scien.cx/2016/01/20/how-to-work-with-json-data-using-python/>
VANCOUVER
Abder-Rahman Ali | Sciencx - » How to Work With JSON Data Using Python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2016/01/20/how-to-work-with-json-data-using-python/
CHICAGO
" » How to Work With JSON Data Using Python." Abder-Rahman Ali | Sciencx - Accessed . https://www.scien.cx/2016/01/20/how-to-work-with-json-data-using-python/
IEEE
" » How to Work With JSON Data Using Python." Abder-Rahman Ali | Sciencx [Online]. Available: https://www.scien.cx/2016/01/20/how-to-work-with-json-data-using-python/. [Accessed: ]
rf:citation
» How to Work With JSON Data Using Python | Abder-Rahman Ali | Sciencx | https://www.scien.cx/2016/01/20/how-to-work-with-json-data-using-python/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.