How we can convert Python objects into JSON objects?


JSON can be abbreviated as JavaScript Object Notation. Json means a script of a text file in a programming language to transfer and store the data.

  • Json supported by the python programming language using a built-in package named json.

  • The Json text is given in the quoted string format which contains in the key and value within the curly braces{}.

  • This looks like a dictionary format in python programming language.

For using the json package in the python programming language we have to import the json package in python script.

In the Json package we have so many methods, among them one of the methods is dumps. This is used to convert the python tuple objects into the Java objects. This is a class of the dump method in Json Package. The dumps method is used to when the data to be in string format and required to be printed whereas the dump is used to store the data.

Syntax

The following is the syntax of the dumps method of the json package that we use in the python programming language

variable_name = json.dumps(object.__dict__)

Where,

  • variable_name is the name of the variable.

  • json is the name of the package.

  • dumps is the name of the method.

  • object is the object to be converted in the json format.

  • __dict__ is the dictionary format function.

Example

Let’s see an example to understand the process to dump the python object into the Json format. The following code can be used to dump the code.

import json
class Student:
   def __init__(self, roll_no, name, batch):
      self.roll_no = roll_no
      self.name = name
      self.batch = batch
s1 = Student("85", "Swapna", "ECE")
jsondump = json.dumps(s1.__dict__)
print(jsondump)

Let’s see the code in detail. We imported the json package at first for using it in python programming language. After that created a class object name Student. After that defined the __init__ function using the attributes self, rll_no, name, batch.

After that assigned the attribute values to the variable name s1. Next assigned the json dumped output to the variable jsondump. Then printed the output stored in jsondump.

Output

The following is the output of the json package dumps method. In the output we can see the converted json format of the object.

{"roll_no": "85", "name": "Swapna", "batch": "ECE"}

Example

Let’s see another example to understand the process to dump the python object into the Json format. The following code can be used to dump the code.

import json
class Car:
   def __init__(self,brand,year):
      self.brand = brand
      self.year =year

s1 = Car("Hyundai", 2010)
s2 = Car("Tata", 2006)
jsondump1 = json.dumps(s1.__dict__)
jsondump2 = json.dumps(s2.__dict__)
print(jsondump1)
print(jsondump2)

Let’s see the code in detail. We imported the json package at first for using it in python programming language.

  • After that created a class object name Car. After that defined the __init__ function using the attributes self, brand and year.

  • After that assigned the attribute values to the variable names s1, s2. Next assigned the json dumped output to the variable jsondump1.

  • Then printed the output stored in jsondump1 and jsondump2.

Output

The following is the output of the json package dumps method. In the output we can see the converted json format of the object.

{"brand": "Hyundai", "year": 2010}
{"brand": "Tata", "year": 2006}

Example

Let’s see another example to understand the process to dump the python object into the Json format. The following code can be used to dump the code.

import json
class food:
   def __init__(self,name):
      self.name = name

s1 = food("biryani")
s2 = food("sweet")
jsondump1 = json.dumps(s1.__dict__)
jsondump2 = json.dumps(s2.__dict__)
print(jsondump1)
print(jsondump2)

Let’s see the code in detail. We imported the json package at first for using it in python programming language. After that created a class object name food. After that defined the __init__ function using the attributes self, name.

After that assigned the attribute values to the variable names s1, s2. Next assigned the json dumped output to the variable jsondump1. Then printed the output stored in jsondump1 and jsondump2.

Output

The following is the output of the json package dumps method. In the output we can see the converted json format of the object.

{"name": "biryani"}
{"name": "sweet"}

Updated on: 15-May-2023

564 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements