Python JSON : How to Use JSON in Python

Jan 19, 2023

1 min read

Published in

What is JSON?

JSON stands for JavaScript Object Notation. It is a lightweight data-interchange structure that is effortless for humans to read and write and for devices to parse and generate.

JSON data is represented as a collection of name-value pairs, where the names are strings, and the values can be numbers, strings, arrays, booleans, null or other JSON objects.

Here are the topis to use JSON in Python.

  • Python has in-build JSON library
  • JSON Data to Python Object

Python has in-build JSON library.

Importing JSON module in Python program

1
import json

In Python, the JSON module provides functions for working with JSON. The module can be used to encode and decode JSON data.

The json.dump() function can be used to write JSON data to a file, and the json.load() function can be used to read JSON data from a file.

The json.dumps() function can be used to convert a Python object into a JSON string, and the json.loads() function can be used to convert a JSON string into a Python object.

JSON Data to Python Object

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
import json

# JSON string:
x =  '{"firstName":"Tom","lastName":"Cruise"}'

# parse and converting to python object:
y = json.loads(x)

# converted Python dictionary object:
print(y["firstName"])

Know more about how to use JSON into Python

Sharing is caring!