python json

Python JSON

JavaScript Object Notation commonly known as Python JSON is a lightweight information interchange format that is usually inspired by using JavaScript item literal syntax. JSON is straightforward for human beings to examine and write. It is also very clean for computers to parse and generate data in a specific manner. A coder can also use JSON for storing and replacing information in much the equal manner as he uses XML.

We can simply import from python using β€˜import json’.

Convert JSON to Python

import json
a =  '{ "name":"Megha", "age":28, "city":"Chandigarh"}'
b = json.loads(a)

print(b["age"])


Output

28

From Python’s factor of view, this JSON is a nested dictionary. JSON is a very famous layout that is often used in web packages. you’ll locate that understanding the way to engage with the usage of Python is beneficial to your personal work.

Encoding a JSON String

Python’s JSON module makes use of dumps() to serialize an item to a string. The “s” in dumps() stands for “string”. It’s less complicated to see how this works by using the usage of the JSON module in some code.

Here we had used json.dumps(), which simply transforms the Python dictionary into a JSON String. The instance’s output also became changed and modified to wrap the string for print. So, in any other case, the string might be a one-liner.

Decoding JSON

Interpreting or deserializing a JSON string is by the loads() technique. masses() is the accomplice feature to dumps(). Here is an example to understand encoding and decoding of JSON:

import json
  
py_object = {"x": 0, "y": 0, "z": 0} 
  
json_string = json.dumps(py_object)
print(json_string)
print(type(json_string))
  
py_obj = json.loads(json_string) 
print()
print(py_obj)
print(type(py_obj))


Output

{"x": 0, "y": 0, "z": 0}
<class 'str'>

{'x': 0, 'y': 0, 'z': 0}
<class 'dict'>

Serialising JSON Data

Serialization will convert your objects into JSON format according to this table. Serialising JSON data comes to play when a computer processes lots of information. It needs to take a data dump. Accordingly, the JSON library exposes the dump() method for writing data to files. There is also a dumps() method (pronounced as β€œdump-s”) for writing to a Python string.

Simple objects are translated to JSON according to a fairly intuitive conversion.

json_str = json.dumps(data, indent=4)

This will change how many spaces are there for indentation as we have 4 here, which can make our JSON easier to read.

Conclusion

We use JSON format very frequently when running with web APIs and web frameworks. The language offers a pleasant tool so as to use to convert JSON to Python objects and again once more in the JSON library. JSON is something more important that a user can easily use in Python and it can make coding a little bit easier.

Discover Our Exciting Courses and Quiz

Enroll now to enhance your skills and knowledge!

Python Online Quiz

Level up your coding skills with our interactive programming quiz!