JSON

Python - A Quick Start for existing Programers

1 min read

Published Sep 16 2025, updated Sep 30 2025


21
0
0
0

Python

Python has a built in module called json for handling the conversion of json to Python objects and serialising Python objects to json.

It can be imported by:

import json



Converting between JSON and Python

There are the following methods:

  • Serialise (Python → JSON string):
    • json.dumps(obj) → Python object → JSON string
    • json.dump(obj, file) → write JSON directly to a file
  • Deserialise (JSON string → Python):
    • json.loads(json_string) → JSON string → Python object
    • json.load(file) → read JSON directly from a file

JSON ↔ Python type mappings:

JSON

Python

Object {}

dict

Array []

list

String "a"

str

Number

int/float

true/false

True/False

null

None


Formatting options

  • indent=4 → pretty-print JSON with indentation
  • sort_keys=True → sort dictionary keys in output
  • separators=(',', ': ') → control formatting


Examples:

import json

# ----------------------------------------
# Python object
data = {
    "name": "Alice",
    "age": 30,
    "is_admin": True,
    "skills": ["Python", "Django", "Flask"],
    "projects": None
}

# ----------------------------------------
# Python -> JSON string
json_string = json.dumps(data)
print("JSON string:", json_string)

# Pretty print JSON
pretty_json = json.dumps(data, indent=4, sort_keys=True)
print("Pretty JSON:\n", pretty_json)

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

# ----------------------------------------
# JSON string -> Python
parsed = json.loads(json_string)
print("Parsed back to Python:", parsed)
print("Name:", parsed["name"])

# Read JSON from file
with open("data.json", "r") as f:
    from_file = json.load(f)
    print("From file:", from_file)

# ----------------------------------------
# Controlling separators (compact JSON)
compact_json = json.dumps(data, separators=(',', ':'))
print("Compact JSON:", compact_json)

Products from our shop

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Mug

Docker Cheat Sheet Mug

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Mug

Vim Cheat Sheet Mug

SimpleSteps.guide branded Travel Mug

SimpleSteps.guide branded Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - Black

Developer Excuse Javascript Mug - Black

SimpleSteps.guide branded stainless steel water bottle

SimpleSteps.guide branded stainless steel water bottle

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Dark

Developer Excuse Javascript Hoodie - Dark

© 2025 SimpleSteps.guide
AboutFAQPoliciesContact