DEV Community

Tommi k Vincent
Tommi k Vincent

Posted on

Python JSON

In this article you will learn how to parse json in python with help of examples.

Definition of JSON:( java-script object notation) is a popular data format used for representing structured data.
it commonly transmits and receives data between a server and a web application in json format.

1 Parse JSON in Python

import json 


person = '{"name":"Bob","langauages":["English","French"]}'

1 #converting the file to json  data 

person_dict = json.loads(person)
#converting the json file to dictionary

print(person_dict)
Enter fullscreen mode Exit fullscreen mode

Here is explanation of the code:
We import the json module, which provides functions for working with JSON data and a JSON string called person, which represents information about a person. It contains two key-value pairs: "name" and "languages". The "languages" value is an array containing two elements: "English" and "French".

json.loads(): a function to parse the JSON string and convert it into a Python dictionary. This function takes the JSON string as input and returns a dictionary representation of the JSON data.

person_dict : we print (person_dict) that displays the contents of the dictionary, showing the key-value pairs and their corresponding values.

output:

Output: {'name': 'Bob', 'languages': ['English', 'French']}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)