Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
June 30, 2022 04:21 pm

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.











































JSONPython
objectdict
arraylist
stringstr
number (int)int
number (real)float
trueTrue
falseFalse
nullNone

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.











































PythonJSON
dictobject
listarray
strstring
intnumber (int)
floatnumber (real)
Falsefalse
Truetrue
Nonenull

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.



Original Link: https://code.tutsplus.com/tutorials/how-to-work-with-json-data-using-python--cms-25758

Share this article:    Share on Facebook
View Full Article

TutsPlus - Code

Tuts+ is a site aimed at web developers and designers offering tutorials and articles on technologies, skills and techniques to improve how you design and build websites.

More About this Source Visit TutsPlus - Code