JavaScript Object Notation (JSON)
JavaScript Object Notation, or JSON for short, is a lightweight, text-based data interchange format that’s easy for humans to read and write, and easy for machines to parse and generate.
Despite its name suggesting a connection to JavaScript, JSON is language-independent and is supported by most modern programming languages, including Python.
JSON Syntax Example
{
"name": "John Doe",
"age": 30,
"is_developer": true,
"skills": ["Python", "JavaScript", "SQL"],
"address": {
"street": "123 Main St",
"city": "Anytown",
"postal_code": "12345"
},
"phone": null
}
Core Concepts
JSON represents data using two structures:
- Collections of key-value pairs: Similar to Python dictionaries, objects in other languages, or hash tables.
- Ordered lists of values: Similar to Python lists, arrays in other languages.
Data Types
JSON supports six data types:
- String - Text enclosed in double quotes:
"Hello World"
- Number - Integer or floating-point numbers:
42
,3.14159
- Boolean - Boolean values:
true
orfalse
(lowercase) - Null - Null value:
null
- Object - Unordered collection of key-value pairs:
{"name": "Python", "version": 3.12}
- Array - Ordered list of values:
[1, 2, 3]
or["apple", "banana", "cherry"]
Syntax Rules
- Data is represented in key-value pairs
- Data items are separated by commas
- Objects are enclosed in curly braces
{}
- Arrays are enclosed in square brackets
[]
- Keys must be strings and are always enclosed in double quotes
- Values can be any valid JSON data type
Python Integration
Python’s built-in json
module provides tools to work with JSON data:
json.dumps()
: Serializes Python objects to JSON stringsjson.loads()
: Deserializes JSON strings to Python objectsjson.dump()
: Writes JSON data to a filejson.load()
: Reads JSON data from a file
Key Differences From Python
While JSON syntax resembles Python, there are important differences:
- JSON uses
null
instead of Python’sNone
- JSON Booleans are
true
orfalse
(lowercase) vs Python’sTrue
orFalse
- JSON requires double quotes for strings while Python allows single or double quotes
- JSON doesn’t support comments
- JSON keys must be strings while Python dictionary's keys can be various types
Common Use Cases
- Web APIs and RESTful services
- Configuration files
- Data storage and exchange between systems
- Client-server communication
- NoSQL database storage formats
Best Practices
- Keep JSON structures simple and flat when possible
- Use descriptive key names
- Validate JSON before parsing because malformed JSON will raise exceptions
- Consider using JSON Schema for validation of complex structures
- Be mindful of number precision limitations across different systems
Related Resources
Tutorial
Working With JSON Data in Python
Learn how to work with JSON data in Python using the json module. Convert, read, write, and validate JSON files and handle JSON data for APIs and storage.
For additional information on related topics, take a look at the following resources:
- Dictionaries in Python (Tutorial)
- Python's list Data Type: A Deep Dive With Examples (Tutorial)
- Python and MongoDB: Connecting to NoSQL Databases (Tutorial)
- Working With JSON in Python (Course)
- Working With JSON Data in Python (Quiz)
- Using Dictionaries in Python (Course)
- Dictionaries in Python (Quiz)
- Exploring Python's list Data Type With Examples (Course)