How to print Python dictionary into JSON format?



The Python Dictionary can be easily displayed into JSON format using the json Python Module. The json module is a JSON encoder/decoder. JSON is JavaScript Object Notation, a lightweight text-based open standard designed for human-readable data interchange. The JSON format was specified by Douglas Crockford. It has been extended from the JavaScript scripting language.

Consider Dictionary as a set of key: value pairs, with the requirement that the keys are unique (within one dictionary). Each key in a Dictionary is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces.

Let us first create a Python Dictionary and fetch all the values. Here, we have included 4 key-value pairs in the Dictionary and displayed them. Product, Model, Units, and Available are keys of the Dictionary. Except the Units key, all are having String values −

Example

# Creating a Dictionary with 4 key-value pairs myprod = { "Product":"Mobile", "Model": "XUT", "Units": 120, "Available": "Yes" } # Displaying the Dictionary print(myprod) # Displaying individual values print("Product = ",myprod["Product"]) print("Model = ",myprod["Model"]) print("Units = ",myprod["Units"]) print("Available = ",myprod["Available"])

Output

{'Product': 'Mobile', 'Model': 'XUT', 'Units': 120, 'Available': 'Yes'}
Product =  Mobile
Model =  XUT
Units =  120
Available =  Yes

Above, we have displayed the 4-key-value pairs in a Dictionary with Product Information. Now, we will see the two ways to update Dictionary values in Python. Now, we will set the Dictionary to the JSON format.

Print the Dictionary into JSON format using the dumps() method

The dumps() function of the json module is used to return a JSON string representation of Python dictionary object. The parameter of the dumps() is the Dictionary −

Example

import json # Creating a Dictionary with 4 key-value pairs myprod = { "Product":"Mobile", "Model": "XUT", "Units": 120, "Available": "Yes" } # Converting to JSON format myJSON = json.dumps(myprod) # Displaying the JSON format print("\nJSON format = ",myJSON);

Output

JSON format = {"Product": "Mobile", "Model": "XUT", "Units": 120, "Available": "Yes"}

Print the Dictionary into JSON object using the __str__(self) method

The __str___(self) function is used to return the string representation of the object. We have declared a class here and used it for the string representation to convert it into json object −

Example

import json # Creating a Dictionary myprod = { "Product":"Mobile", "Model": "XUT", "Units": 120, "Available": "Yes" } # Declared a class class myfunc(dict): def __str__(self): return json.dumps(self) myJSON = myfunc(myprod) print("\nJSON format = ",myJSON);

Output

JSON format = {"Product": "Mobile", "Model": "XUT", "Units": 120, "Available": "Yes"}

Print the Dictionary into JSON array

An array can be converted to a JSON object. We will set the keys and values in an array and use the dump() method −

Example

import json # Creating a Dictionary myprod = { "Product":"Mobile", "Model": "XUT", "Units": 120, "Available": "Yes" } # Keys and Values of a Dictionary in an array arr = [ {'key' : k, 'value' : myprod[k]} for k in myprod] # Displaying the JSON print("\nJSON format = ",json.dumps(arr));

Output

JSON format = [{"key": "Product", "value": "Mobile"}, {"key": "Model", "value": "XUT"}, {"key": "Units", "value": 120}, {"key": "Available", "value": "Yes"}]

Advertisements