Skip to content

What Are the Differences Between Python Dictionary and JSON

Learn how Python dictionaries (in-memory objects with flexible key types) differ from JSON (text-based data format with strict string keys and serialization rules), including conversion methods and common pitfalls.

Python dictionaries and JSON look nearly identical at first glance, both using curly braces and key-value pairs. This visual similarity causes frequent confusion, but they serve fundamentally different purposes. A dictionary is a data structure that lives in your program's memory. JSON is a text format designed for exchanging data between systems. Understanding the distinction is essential when working with APIs, configuration files, and data storage.

This guide explains the key differences, shows how to convert between the two formats, and covers common pitfalls that trip up developers.

Python Dictionary: An In-Memory Data Structure

A dictionary is a native Python object stored in RAM. It maps keys to values, supports various hashable types as keys, and can hold references to any Python object, including functions, classes, and custom instances:

from datetime import datetime

data = {
    "name": "Alice",
    1: "integer key",            # Non-string keys are valid
    (2, 3): "tuple key",         # Any hashable type works
    "active": True,              # Python boolean (capital T)
    "data": None                 # Python None (capital N)
}

print(type(data))
print(data[1])
print(data[(2, 3)])

Output:

<class 'dict'>
integer key
tuple key

Dictionaries can also store complex Python objects that have no equivalent in other languages:

from datetime import datetime

user = {
    "name": "Bob",
    "created": datetime.now(),      # datetime object
    "process": lambda x: x * 2,     # function reference
    "scores": {10, 20, 30}          # set
}

print(type(user["created"]))
print(user["process"](5))

Output:

<class 'datetime.datetime'>
10

JSON: A Text Serialization Format

JSON (JavaScript Object Notation) is a string designed for data interchange between systems, languages, and platforms. It follows strict syntax rules that differ from Python in several important ways:

import json

json_string = '{"name": "Alice", "age": 30, "active": true}'

print(type(json_string))

parsed = json.loads(json_string)
print(type(parsed))

Output:

<class 'str'>
<class 'dict'>

JSON has specific formatting requirements:

  • Keys must be strings enclosed in double quotes
  • String values must use double quotes (single quotes are invalid)
  • Booleans are lowercase: true and false
  • Null values use the keyword null
  • No trailing commas are allowed

Converting Between Dictionaries and JSON

The built-in json module provides two primary functions for conversion:

  • json.dumps(): Converts a Python dictionary to a JSON string (serialization)
  • json.loads(): Converts a JSON string to a Python dictionary (deserialization)
import json

user_dict = {"name": "Alice", "age": 30, "active": True, "data": None}
json_string = json.dumps(user_dict)
print(f"JSON string: {json_string}")
print(f"Type: {type(json_string)}")

print()

parsed_dict = json.loads(json_string)
print(f"Dictionary: {parsed_dict}")
print(f"Type: {type(parsed_dict)}")

Output:

JSON string: {"name": "Alice", "age": 30, "active": true, "data": null}
Type: <class 'str'>

Dictionary: {'name': 'Alice', 'age': 30, 'active': True, 'data': None}
Type: <class 'dict'>

Notice how Python's True becomes JSON's true, and Python's None becomes JSON's null. These conversions happen automatically in both directions.

Handling Non-Serializable Types

JSON only supports a small set of basic types. If your dictionary contains Python-specific objects like datetime, set, or custom classes, json.dumps() will raise a TypeError. You need to provide a custom encoder to handle these types:

import json
from datetime import datetime

data = {
    "event": "login",
    "timestamp": datetime.now()
}

try:
    json.dumps(data)
except TypeError as e:
    print(f"Error: {e}")

def json_encoder(obj):
    if isinstance(obj, datetime):
        return obj.isoformat()
    raise TypeError(f"Object of type {type(obj)} is not JSON serializable")

result = json.dumps(data, default=json_encoder)
print(result)

Output:

Error: Object of type datetime is not JSON serializable
{"event": "login", "timestamp": "2026-02-18T21:38:10.125495"}

Key Pitfall: Integer Keys Become Strings

JSON only supports string keys. When you serialize a dictionary with non-string keys, they are automatically converted to strings. This means a round-trip conversion does not preserve the original key types:

import json

original = {1: "one", 2: "two", 3: "three"}
print(f"Original keys: {list(original.keys())}")
print(f"Key types: {[type(k) for k in original.keys()]}")

json_string = json.dumps(original)
restored = json.loads(json_string)

print(f"\nRestored keys: {list(restored.keys())}")
print(f"Key types: {[type(k) for k in restored.keys()]}")

try:
    print(restored[1])
except KeyError:
    print("\nKeyError: integer key 1 no longer exists")
    print(f"Must use string key: {restored['1']}")

Output:

Original keys: [1, 2, 3]
Key types: [<class 'int'>, <class 'int'>, <class 'int'>]

Restored keys: ['1', '2', '3']
Key types: [<class 'str'>, <class 'str'>, <class 'str'>]

KeyError: integer key 1 no longer exists
Must use string key: one

Type Mapping Reference

Python Type JSON Type Notes
dict object Keys are converted to strings
list, tuple array Tuples become arrays (lists on round-trip)
str string Must use double quotes in JSON
int, float number JSON makes no distinction between int and float
True true Lowercase in JSON
False false Lowercase in JSON
None null Different keyword

Feature Comparison

Feature Dictionary JSON
What it is In-memory Python object Text string for data exchange
Key types Any hashable type Strings only
Value types Any Python object Strings, numbers, booleans, null, arrays, objects
Quote style Single or double Double quotes only
Trailing commas Allowed Invalid
Boolean format True / False true / false
Null format None null
Primary purpose Program logic and data manipulation Data interchange between systems

Summary

A Python dictionary is an in-memory object that you manipulate directly in your code. JSON is a text string that you send over networks, store in files, or use to exchange data between different systems and programming languages.

  • You cannot "parse" a dictionary because it is already a live Python object.
  • You parse JSON strings into dictionaries using json.loads(), and convert dictionaries into JSON strings using json.dumps().

When converting between the two, be aware of the automatic type transformations:

  • True becomes true
  • None becomes null
  • integer keys become string keys
  • tuples become arrays

Understanding these differences prevents confusion and subtle bugs when working with APIs, configuration files, and data storage.

Navigation