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

JSON
{
  "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:

  1. Collections of key-value pairs: Similar to Python dictionaries, objects in other languages, or hash tables
  2. 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 or false (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 strings
  • json.loads(): Deserialize JSON strings to Python objects
  • json.dump(): Write JSON data to a file
  • json.load(): Read JSON data from a file

Key Differences From Python

While JSON syntax resembles Python, there are important differences:

  • JSON uses null instead of Python’s None
  • JSON Booleans are true or false (lowercase) vs Python’s True or False
  • JSON requires double quotes for strings while Python allows single or double
  • 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

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.

intermediate python

For additional information on related topics, take a look at the following resources:


By Dan Bader • Updated Aug. 4, 2025 • Reviewed by Leodanis Pozo Ramos