Ujson | Python

In this tutorial, we will learn about UJSON in Python. Before learning about UJSON, we have to understand the concept of  JSON.


JSON is a format in which you can transfer the data from client to server or vice-versa. Previously, when data sent from server to client it in is a text format. So when client wants to fetch some data from a text file they have to check every string in a text file.  So developer comes with new format i.e XML format. But it is also so hard to fetch the data from XML file because of its complex format. So the developer develops a new format i.e JSON. Every browser supports the javascript so it is easy to fetch the data and JSON stores the data in JSON string format under curly braces in the key and name format. UJSON is an upgraded version of JSON.

JSON string format:

"name": "Horseman",
"age": 29,
"secretIdentity": "Dan Jukes",
"powers":  "Fire"

Above example looks like a python dictionary, but there is a difference between python dictionary and a JSON string, which we will see in the program.

UJSON in Python

UJSON stands on Ultra JavaScript Object Notation. UJSON is the ultra-fast JSON encoder and decoder which written in pure C language.

Function of the UJSON:

  • loads() = Its loads UJSON  data and convert into dictionary.
  • dumps() =  Its loads python dictionary and convert into UJSON strings.
import ujson

a = {"name":"Horseman", "age": "21", "city": "Mumbai"}
print("a type:", type(a))
b1=ujson.dumps(a, indent=4)
print("b variable:", type(b))
c = ujson.loads(b)
print("c variable:", type(c))


a type: <class 'dict'>
b variable: <class 'str'>
c variable: <class 'dict'>
{'name': 'Horseman', 'age': '21', 'city': 'Mumbai'}

Working with the file:

When we work with the file we will use load() and dump() function.

Steps 1: Import ujson library, open the file and stores data in the file variable.
Steps 2: Load the file in the SuperHeroes variable using ujson.load(file) which convert the string into a python dictionary.
Steps 3: To read the data of a file in UJSON format use ujson.dumps(SuperHeroes, indent=4) and Store in PySuperHeroes variable and print the PySuperHeroes variable.
Steps 4: Now fetch the data from file.

import ujson

file = open("JsonExample.json", "r")
SuperHeros = ujson.load(file)
PySuperHeroes = ujson.dumps(SuperHeros, indent=4)

Explanation of fetching the data:

  • Here, we have to find the 3 power of SuperHero name “Madame Uppercut” i.e Superhuman reflexes.
  • So we have to access the members’ property from the SuperHeroes variable, so we used SuperHeroes[“members”].
  • Member contains much info but we want to access the Madame Uppercut which is in 2 positions. So we used SuperHeroes[“members”][1].
  • Now we want 3 power of Madame Uppercut. So we used SuperHeroes[“members”][1][“power”][2].


     "squadName":"Super hero squad",
     "homeTown":"Metro City",
     "secretBase":"Super tower",
               "name":"Molecule Man",
               "secretIdentity":"Dan Jukes",
                         "Radiation resistance",
                         "Turning tiny",
                         "Radiation blast"
              "name":"Madame Uppercut",
               "secretIdentity":"Jane Wilson",
                       "Million tonne punch",
                       "Damage resistance",
                       "Superhuman reflexes"
              "name":"Eternal Flame",
                       "Heat Immunity",
                       "Interdimensional travel"

Data fetch: Superhuman reflexes

You can also learn:

Get the position of max value in a list in Python
How to find roots of polynomial in Python
numpy.squeeze() in Python
How to find the system time in Python

One response to “Ujson | Python”

  1. jose luis garcia says:

    Thanks!!! I didn’t know how to show the json, 1 whole day lost. Thanks!!

Leave a Reply

Your email address will not be published. Required fields are marked *