Open In App

How to convert Ordereddict to JSON?

Last Updated : 02 Feb, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn How to convert a nested OrderedDict to JSON? Before this we must go through  some concepts:

  • The full-form of JSON is JavaScript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a built-in package called JSON. To use this feature, we import the JSON package in Python script. 
  • The text in JSON is done through quoted-string which contains a value in key-value mapping within { }. It is similar to the dictionary in Python. JSON shows an API similar to users of Standard Library marshal and pickle modules and Python natively supports JSON features
  • An OrderedDict is a dictionary subclass that remembers the order that keys were first inserted. The only difference between dict and OrderedDict() is that:
  • OrderedDict preserves the order in which the keys are inserted. A regular dict doesn’t track the insertion order, and iterating it gives the values in an arbitrary order. By contrast, the order the items are inserted is remembered by OrderedDict.

To define the OrderedDict, we are using the collections module in python.

Python3




# import package
from collections import OrderedDict
  
# define OrderedDict
od1 = OrderedDict([('1','one'), 
                   ('2','two')])
  
# display dictionary
print(type(od1))
print(od1)


Output:

<class 'collections.OrderedDict'>
OrderedDict([('1', 'one'), ('2', 'two')])

To convert OrderedDict to JSON, we are using json.dumps()

  • The full-form of JSON is JavaScript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a built-in package called JSON. To use this feature, we import the JSON package in Python script. The text in JSON is done through quoted-string which contains the value in key-value mapping within { }. It is similar to the dictionary in Python.
  • The json.dumps() function converts a Python object into a JSON string.

Python3




# import package
from collections import OrderedDict
import json
  
# define OrderedDict
od1 = OrderedDict([('1','one'), 
                   ('2','two')])
  
# check type i.e; OrderedDict
print(type(od1))
  
# convert to json
od1 = json.dumps(od1)
  
# check type i.e; str
print(type(od1))
  
# view value
print(od1)


Output

<class 'collections.OrderedDict'>
<class 'str'>
{"1": "one", "2": "two"}

We can give indent value to show the dictionary pattern.

Python3




# import package
from collections import OrderedDict
import json
  
# define OrderedDict
od1 = OrderedDict([('1', 'one'),
                   ('2', 'two')])
  
# check type i.e; OrderedDict
print(type(od1))
  
# convert to json
od1 = json.dumps(od1, indent=4)
  
# check type i.e; str
print(type(od1))
  
# view value
print(od1)


Output:

<class 'collections.OrderedDict'>
<class 'str'>
{
    "1": "one",
    "2": "two"
}


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads