Introduction to Python JSON Object Conversion

In this article you will learn about Python JSON object conversion.

Python Json object – Before moving ahead let’s know a little bit about Python Json

JSON – It is Python built-in package, which is used in Python to work with data related to JSON. JSON is text and written in javascript format.

– When a Python object is converted to Json object it will be equal to –

PythonJSON
dict
list
tuple
str
int
float
True
False
None
Object
Array
Array
String
Number
Number
true
false
null

Example 1- Convert all Python object (data types) into Json object.

import json

x = {"name" : "John" ,
"age" : 30 ,
"Job" : True,
"Education" : False,
"Id section" : (1,2,3),
"Teaching" : None,
"students" : [
{"name" : "Alex" , "Add" : "NYC" },
{"name" : "Jew" , "Add" : "UK" }]}
 
print (json.dumps(x)) 
python-json-object

Output-

python-json-object

Explanation – As shown clearly, it converted the Python dict list into JSON string then returned the whole dict.

Note: The above example prints Json Sting but it is without any rules of Python such as indentation, breaking lines, etc., therefore it’s difficult to read it.

— By using json.dumps() method’s parameter it can be read it easily.

Example 2- Use the indent parameter to define the numbers of indents.

import json

x = {"name" : "John" ,
"age" : 30 ,
"Job" : True,
"Education" : False,
"Id section" : (1,2,3),
"Teaching" : None,
"students" : [
{"name" : "Alex" , "Add" : "NYC" },
{"name" : "Jew" , "Add" : "UK" }]}
z= json.dumps(x, indent= 4 )

print(z) 
python-json-object

Output –

python-json-object

Explanation – As it is shown clearly that it added 4 whitespaces before to returned whole dict. 

Here are some separator which can also be used as parameters. The default value is (“, “, “: “), which means using a comma and a space to separate each object, and a colon and space to separate keys from values:

Example 3- Use of the separators as parameter to change the default separator.

import json

x = {"name" : "John" ,
"age" : 30 ,
"Job" : True,
"Education" : False,
"Id section" : (1,2,3),
"Teaching" : None,
"students" : [
{"name" : "Alex" , "Add" : "NYC" },
{"name" : "Jew" , "Add" : "UK" }]}
z = json.dumps(x, indent= 4 , separators=( ". " , " == " ))

print(z) 
python-json-object

Output –

python-json-object
import json

x = {"name" : "John" ,
"age" : 30 ,
"Job" : True,
"Education" : False,
"Id section" : (1,2,3),
"Teaching" : None,
"students" : [
{"name" : "Alex" , "Add" : "NYC" },
{"name" : "Jew" , "Add" : "UK" }]}
z = json.dumps(x, indent= 4 , sort_keys=True)

print(z) 
python-json-object

Output –

python-json-object

Explanation – As it is shown clearly that it returned sorted dict according to alphabetically. In other words, it sorted key first according to upper case and then lower case.

Note: Python is case sensitive language. Upper case and lower case are treated in Python differently.

If you find anything incorrect in the above-discussed topic and have any further questions, please comment down below.

Like us on

Leave a Comment

Stay in the loop

codingstreets