Here’s How To Convert Python Dictionary To JSON?

JSON file represents JavaScript Object Notation. It implies that a content (executable) document which is made of text in a programming language, is utilized to store and move the information. Python upholds JSON through an inherent bundle called JSON. To utilize this component, we import the JSON bundle in Python script. The text in JSON is done through the cited string which contains a worth in key-esteem planning inside { }. It is like the word reference in Python.

JSON (JavaScript Object Notation) is a well known document arrangement to introduce the organized information and move the information between the server and the application without any problem. The construction of this document is like some Python objects like rundown, tuple, and word reference. You can change over any word reference object into the JSON object by utilizing dump() and dumps() techniques for the json module. This article shows the ways of changing over any word reference object to the JSON object in python.

What is JSON in Python?

JSON(Javascript Object Notation) is a standard arrangement to move the information as a message that can be sent over an organization. JSON is a punctuation for trading and putting away information over the organization. It utilizes bunches of APIs and information bases that are simple for people and machines to peruse and comprehend. Python has an inbuilt bundle named ‘json’, which you can use to work with JSON information. To utilize this element, you need to import the JSON bundle in python programming.

Python JSON stores the information as key-esteem sets inside wavy brackets({}), and henceforth, it is really like a python word reference. In any case, here, the JSON key is a string object with twofold quote necessarily. Nonetheless, the worth comparing to the key could be of any information type, i.e., string, number, settled JSON, or some other succession information type like an exhibit.

For Example

import json
# some JSON:
a =  '{ "name":"Jack", "age":21, "city":"California"}'
# parse x:
b = json.loads(a)



Remember that the JSON exists as a string but not a string from the data context.

Note: For more data, allude to Read, Write and Parse JSON utilizing Python

What is Dictionary in Python?

Word reference is one of the information types in python used to store the arrangement of information in a solitary variable. Python word reference helps store the information esteems like a guide that isn’t upheld by some other information type which holds just a solitary worth as a component. Word reference is an unordered and variable assortment of information components put away as key:value sets inside the wavy brackets({}). Here the colon(:) addresses the key related with its individual worth.

Word reference esteems can be of any information type and permit copy esteems, while word reference keys are interesting and changeless.

Work Used:

Consideration nerd! Fortify your establishments with the Python Programming Foundation Course and get familiar with the rudiments.

Regardless, your meeting arrangements Enhance your Data Structures ideas with the Python DS Course. Furthermore, regardless of your Machine Learning Journey, join the Machine Learning – Basic Level Course

  • JSON.dumps()
  • JSON.dump()
  • Language structure: JSON.dumps(dict, indent)
  • Boundaries:
  • word reference – the name of word reference which ought to be changed over to JSON object.
  • indent – characterizes the number of units for space
  • Sentence structure: JSON.dump(dict, file_pointer)
  • Boundaries:
  • word reference – name of word reference which ought to be changed over to JSON object.
  • document pointer – pointer of the record opened in compose or add mode.

This strategy has numerous contentions like dumps(). The employments of three contentions are utilized in this article to change over the information of a word reference object into JSON information and store the information into a JSON record. Here, the main contention is utilized to take a word reference object that requirements to change over into a JSON object, and the subsequent contention is utilized to take the name of the document overseer of the record where the JSON information will be composed. The third contention is utilized to set the space unit.

How these two strategies can be utilized to change over word reference object into a JSON document or JSON string are displayed underneath of this article.

Model 1: Convert word reference into JSON utilizing dumps() with space

It is referenced before that dumps() strategy has one obligatory boundary and it can take the word reference object to change over the information into JSON string. In the accompanying content, dict_data is a word reference variable that contains the information of a specific understudy record. Right away, the dumps() technique is utilized with one contention and the worth of dict_data is changed over into JSON information. The result of both word reference and JSON design is something similar in case no space is utilized in JSON information. Then, the dumps() technique is utilized with two contentions, and 3 is utilized as a space an incentive for JSON information. The second JSON result will produce with space.

Example 1:

import json 
# Data to be written 
dictionary =
  "id": "04"
  "name": "sunil"
  "department": "HR"
# Serializing json  
json_object = json.dumps(dictionary, indent = 4


    "id": "04",
    "name": "sunil",
    "department": "HR"


    "department": "HR",
    "id": "04",
    "name": "sunil"

Example 2:

import json
# Data to be written
dictionary ={
    "name" : "sathiyajith",
    "rollno" : 56,
    "cgpa" : 8.6,
    "phonenumber" : "9976770500"
with open("sample.json", "w") as outfile:
    json.dump(dictionary, outfile)



Also Read: How to add an element to an Array in Java?

Leave a Reply

Your email address will not be published.