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()
: Serialize Python objects to JSON stringsjson.loads()
: Deserialize JSON strings to Python objectsjson.dump()
: Write JSON data to a filejson.load()
: Read JSON data from a file
Key Differences From Python
While JSON syntax resembles Python, there are important differences:
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
In this tutorial, you'll learn how to read and write JSON-encoded data in Python. You'll begin with practical examples that show how to use Python's built-in "json" module and then move on to learn how to serialize and deserialize custom data.
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)