JSON in Python is a text-based data format (JavaScript Object Notation) that Python handles with the built-in json module to exchange data with files, APIs, and other programs.

what is json in python

Quick Scoop

Think of JSON in Python as a common language that your Python code uses to talk to the outside world—browsers, APIs, config files, and other services—using simple text that looks a lot like a Python dictionary.

JSON in one minute

  • JSON stands for JavaScript Object Notation, but it’s now language‑independent and widely used everywhere.
  • It stores data as key–value pairs, arrays (lists), numbers, strings, booleans, and null.
  • In Python, JSON is usually a string that we convert to and from Python objects (dict, list, etc.).
  • Python’s built-in json module lets you:
    • Parse JSON text into Python objects (json.loads, json.load).
* Convert Python objects into JSON text (`json.dumps`, `json.dump`).

In modern web apps and APIs (2020s onwards), JSON is the default “data language” for requests and responses.

How JSON looks (and how Python sees it)

JSON syntax (general idea)

  • Data is written as key–value pairs: "key": value.
  • Pairs are separated by commas inside {} for objects.
  • Lists of values are inside [] (arrays).
  • Strings must use double quotes in JSON: "text".
  • Booleans are lowercase: true, false, and null (not True, False, None).

Example JSON text (not Python):

json

{
  "name": "Alice",
  "age": 30,
  "is_developer": true,
  "skills": ["Python", "JSON"],
  "address": {
    "city": "London",
    "remote": false
  },
  "twitter": null
}

Python will typically treat that entire thing as a string at first, then you convert it to a Python dict.

JSON vs Python types (mental map)

Here’s a quick view of how types map between JSON and Python.

html

<table>
  <thead>
    <tr>
      <th>Concept</th>
      <th>JSON</th>
      <th>Python</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Object</td>
      <td>{ "key": "value" }</td>
      <td>dict</td>
    </tr>
    <tr>
      <td>Array</td>
      <td>["a", "b", "c"]</td>
      <td>list</td>
    </tr>
    <tr>
      <td>String</td>
      <td>"hello"</td>
      <td>str</td>
    </tr>
    <tr>
      <td>Number (integer)</td>
      <td>42</td>
      <td>int</td>
    </tr>
    <tr>
      <td>Number (float)</td>
      <td>3.14</td>
      <td>float</td>
    </tr>
    <tr>
      <td>Boolean</td>
      <td>true / false</td>
      <td>True / False</td>
    </tr>
    <tr>
      <td>null</td>
      <td>null</td>
      <td>None</td>
    </tr>
  </tbody>
</table>

Note: JSON object keys must be strings; Python dict keys can be many types, but only those that serialize correctly will work as JSON keys.

The json module: your main tool

Python ships with a standard json module for reading and writing JSON.

Core functions

  1. From JSON to Python (decoding / deserialization)
 * `json.loads(json_string)` – parse a JSON **string** into Python.
 * `json.load(file_obj)` – parse JSON **from a file** into Python.
  1. From Python to JSON (encoding / serialization)
 * `json.dumps(python_obj)` – convert a Python object to a JSON **string**.
 * `json.dump(python_obj, file_obj)` – write JSON to a **file**.

Encoding/serialization = Python → JSON; decoding/deserialization = JSON → Python.

Practical examples (short and useful)

1. String to Python dict

python

import json

data = '{"name": "Alice", "age": 30, "is_developer": true}'

py_obj = json.loads(data)
print(py_obj)          # {'name': 'Alice', 'age': 30, 'is_developer': True}
print(type(py_obj))    # <class 'dict'>

This is a classic API-response scenario, where you receive JSON text and turn it into Python data.

2. Python dict to JSON string

python

import json

user = {
    "id": 1,
    "name": "Alice",
    "languages": ["Python", "JavaScript"],
    "active": True,
    "twitter": None
}

json_text = json.dumps(user, indent=4, sort_keys=True)
print(json_text)
  • indent=4 pretty-prints the JSON.
  • sort_keys=True sorts dictionary keys alphabetically.

3. Read/write JSON files

python

import json

config = {
    "debug": True,
    "version": 1,
    "services": ["auth", "billing"]
}

# Write to file
with open("config.json", "w") as f:
    json.dump(config, f, indent=2)

# Read from file
with open("config.json", "r") as f:
    loaded = json.load(f)

print(loaded)

This pattern is common for configuration files, small datasets, or caching API responses.

Why JSON matters in Python today

  • APIs & web dev: Almost all REST APIs in 2020s-era web services send and receive JSON.
  • Microservices & cloud: Services communicate via JSON over HTTP, queues, or event systems.
  • Config & data storage: JSON is often used for simple configuration files, test fixtures, and logs.
  • Data science & tooling: Tools, notebooks, and pipelines frequently use JSON as an interchange format.

You’ll see JSON constantly if you work with web frameworks (FastAPI, Django REST), frontends (React, Vue), or third‑party APIs in 2024–2026.

Common gotchas when using JSON in Python

  1. Double vs single quotes
    • JSON requires double quotes for strings; Python code can use single quotes, but the JSON text must use "...".
  1. Boolean and null values
    • JSON: true, false, null.
    • Python: True, False, None (the json module handles converting between them).
  1. Non-serializable objects
    • You can’t directly json.dumps() things like custom classes, open files, or complex objects without a custom encoder.
 * You usually convert them into basic types first (dict, list, str, int, float, bool, None).
  1. Comments are not allowed in standard JSON
    • If you see “JSON with comments” in some configs, it’s technically not valid JSON and may need pre-processing.

Different viewpoints: how developers use JSON in Python

  • Backend developer’s view :
    JSON is the main way to send data to frontends and mobile apps. They focus on serialization, validation, and API schemas (e.g., FastAPI/Pydantic models producing JSON).
  • Data engineer’s view :
    JSON is a flexible format to read from logs, queues, and external systems. They often stream and transform JSON records in bulk.
  • Beginner’s view :
    JSON feels like a Python dict with slightly stricter syntax and is a gentle introduction to thinking about structured data and APIs.

Tiny “story” example: from browser to Python and back

Imagine you have a React frontend that sends a login request:

  1. The browser sends JSON:

    json
    
    {
      "username": "alice",
      "password": "secret123"
    }
    
  2. Your Python backend receives this as text, then does:

    python
    
    import json
    
    body = request_body_text  # e.g., from a HTTP request
    data = json.loads(body)   # {"username": "alice", "password": "secret123"}
    
  3. After checking credentials, Python responds with JSON:

    python
    
    response = {"success": True, "token": "abc123"}
    response_text = json.dumps(response)
    
  4. The browser gets {"success": true, "token": "abc123"} and uses it to log the user in.

That back-and-forth is JSON in action with Python in the middle doing the logic.

Quick recap (TL;DR)

  • JSON is a text-based, language-neutral data format, heavily used on the web.
  • In Python, you work with JSON via the built-in json module.
  • Use json.loads / json.load to go from JSON to Python; json.dumps / json.dump to go from Python to JSON.
  • JSON is everywhere in modern APIs, configs, and data pipelines, so understanding it is essential for Python developers in 2026.

Information gathered from public forums or data available on the internet and portrayed here.