Understanding JSON in python
JSON (JavaScript Object Notation) is the lightweight and widely used format for storing and exchanging the data. it is easy to read write and process, making it popular choice for web api and data storage. python provide the inbuild Json module to work with Json data efficiently.
What is JSON ?
Json represent as the key-value pairs similar to the python dictionary. It supports data types like string, numbers, Booleans, array(list), and nested objects.
Example of the JSON data:
{
"Name" : "Yokeswaran",
"age" : 22,
"isEmployee" : true,
"Skills" : ["Python","Machine learning","Artificial intelligent","Data Scientist"],
"Adress" : {
"Street" : "Vivekanandhapuram West",
"Town" : "Devakottai",
"Pincode" : 630303
}
This structure consists of:
JSON operation in the python
1. Reading JSON from a File: json.load()
The json.load() function is used to read JSON data from a file and convert it into a Python object (typically a dictionary).
Example:
import json
with open('data.json', 'r') as file:
data = json.load(file) # Reads JSON from the file and converts it into a Python dictionary
print(data)
In this example, the JSON file data.json is opened in read mode. The content is then converted into a Python dictionary stored in the variable data.
2. Converting JSON from a String: json.loads()
When you have a JSON-formatted string (for example, from an API response), json.loads() is used to convert the string into a Python object.
Recommended by LinkedIn
Example:
import json
json_string = '{"name": "yokeswaran", "age": 22, "is_student": false}'
data = json.loads(json_string) # Converts the JSON string into a Python dictionary
print(data)
3. Writing JSON to a File: json.dump()
The json.dump() function serializes a Python object as JSON and writes it directly to a file.
Example:
import json
data = {"name": "Alice", "age": 25, "is_student": False}
with open('output.json', 'w') as file:
# Writes the Python object to a file as formatted as JSON
json.dump(data, file, indent=4)
This example shows how to write the dictionary data to output.json with proper indentation for readability.
4. Converting a Python Object to a JSON String: json.dumps()
If you need a JSON string instead of writing directly to a file, use json.dumps() to convert a Python object to a JSON-formatted string.
Example:
import json
data = {"name": "Alice", "age": 25, "is_student": False}
json_string = json.dumps(data, indent=4) # Converts the Python object to a JSON string
print(json_string)
This example converts the Python dictionary into a well-formatted JSON string and prints it out.
Key Takeaways
Conclusion:
In conclusion, JSON is a lightweight data format that's easy to read and use. Python's json module provides straightforward functions to convert between JSON data and Python objects, making it simple to work with data from files or strings. Whether you're reading configuration files or handling API responses, these tools help streamline your data processing tasks.
Former HPC Intern at Altair Engineering| Backend Developer | Python, Java, DevOps | Linux, Docker, PBS Professional
1mojson.load() , takes json data as I/p and o/p will be python dictionary. Great work
Machine Learning Engineer Intern | AI Enthusiast | "AI Explorer: Mapping the Future of Intelligence"
2moLove this insight
Adaptive and Aspiring software Developer | Seeking Opportunities to Innovate and Grow
2moWell framed.
SOL Student at Inha University in Tashkent
2mo🤔 interesting
Quality Assurance Engineer
2moGreat work 👏