JSON (JavaScript object notation)
Introduction
In modern software development, data exchange between systems is extremely common. Whether you are building a web application, consuming an API, or storing configuration data, you will most likely work with JSON.
In this guide, we will understand:
- What JSON is ?
- Why JSON is important ?
- Valid JSON data types
- Difference between JSON and XML
- How to use JSON in Python
- Convert JSON to Python objects
- Convert Python objects to JSON
- Real-world examples
By the end of this article, you will have a clear understanding of how JSON works with Python in real applications.
What is JSON?
JSON stands for JavaScript Object Notation.
It is a lightweight data format used for:
- Storing data
- Exchanging data between systems
- Sending data in APIs
Although JSON is derived from JavaScript syntax, it is language-independent. Almost every programming language supports JSON.
Why JSON is Important
In real-world software systems:
- Frontend sends data to backend using JSON
- Backend sends API responses in JSON format
- Configuration files often use JSON
- Databases export/import JSON
For example:
When you call a REST API, you typically receive data like this:
{
"name": "Shakti",
"age": 30,
"city": "Bangalore"
}That is JSON.
Important Properties of JSON
JSON follows a very simple structure:
1. Data is stored as key/value pairs
Example:
"name": "Shakti"
Here:
name→ keyShakti→ value
2. Data is separated by commas
{
"name": "Shakti",
"age": 30,
"city": "Bangalore"
}
3. Curly Braces {} Hold Objects
An object is a collection of key/value pairs.
4. Square Brackets [] Hold Arrays
Example:
{ "skills": ["Python", "Java", "Automation"] }
Here, skills is an array.
Valid Data Types in JSON
JSON values must be one of the following:
- String
- Number
- Object
- Array
- Boolean (true/false)
- null
Example covering all types:
{ "name": "John","age": 30,"isEmployee": true,"skills": ["Python", "SQL"],"address": { "city": "New York", "zip": 10001 },"middleName": null }
Important Clarification
Many beginners get confused:
👉 JSON is not a Python dictionary
👉 JSON is a string format
Python dictionaries look similar, but they are not the same thing.
Difference Between JSON and XML
Both JSON and XML are used for data exchange.
XML
- Uses tags
- More verbose
- Requires XML parser
- Larger file size
Example:
<person> <name>John</name> <age>30</age> </person>JSON
- Lightweight
- Easier to read
- Faster to parse
- Uses simple key/value structure
Example:
{ "name": "John", "age": 30 }In modern APIs, JSON is preferred over XML.
Working with JSON in Python
Python provides a built-in module called:
import json
- Parse JSON strings
- Convert Python objects to JSON
- Convert JSON to Python objects
This module helps you:
Convert JSON to Python (Deserialization)
This process is called deserialization.
We use:
json.loads()
Example:
import json x = '{"name" : "Shakti", "age" : 30, "city" : "Bangalore"}' y = json.loads(x) print(y["name"])Output:
Shakti
What happened here?
xis a JSON stringjson.loads()converts it into a Python dictionary- Now we can access it like a normal dict
Convert Python to JSON (Serialization)
This process is called serialization.
We use:
json.dumps()Example:
import json x = { "name": "John", "age": 30, "city": "New York" } y = json.dumps(x) print(y)
Output:
{"name": "John", "age": 30, "city": "New York"}Now it is converted into a JSON string.
Real-World Example: Calling an API in Python
In real projects, you often:
- Call an API
- Receive JSON
- Convert to Python dictionary
- Process data
Example:
import requests import json response = requests.get("https://api.example.com/user") data = response.json() print(data["name"])
Here:
- API returns JSON
- Python automatically converts it into dictionary
- You access fields normally
This is how almost all backend integrations work.
Common Mistakes Beginners Make
- Using single quotes in JSON ❌ 'name': 'John' JSON requires double quotes.
- Forgetting commas between fields
- Confusing Python dict with JSON string
- Trying to access JSON without parsing it first
When Should You Use JSON in Python?
You should use JSON when:
- Working with REST APIs
- Saving configuration data
- Logging structured data
- Storing lightweight data
In automation and testing (especially API testing), JSON is used heavily.
FAQ Section
Is JSON a programming language?
No. JSON is a data format.
Is JSON faster than XML?
Yes, JSON is lighter and faster to parse in most cases.
Can Python directly read JSON files?
Yes.
Example:
with open("data.json") as f: data = json.load(f)
What is the difference between load and loads?
load()→ Reads JSON from fileloads()→ Reads JSON from string
Conclusion
JSON is one of the most important data formats in modern software development.
When working with Python:
- Use
json.loads()to convert JSON string to Python object- Use
json.dumps()to convert Python object to JSON string
Understanding JSON is essential if you are working with APIs, automation, backend systems,
or web applications.
Mastering this concept will make you comfortable working with real-world data exchange
systems.
0 Comments
Post a Comment