Parse a JSON string with json.loads

import json

text = "{"name": "Sam", "age": 30}"
data = json.loads(text)
print(data["name"])   # Sam
print(type(data))     # <class "dict">

json.loads (load string) reads a JSON string and returns the matching Python object — usually a dict or a list.

Read JSON from a file with json.load

import json

with open("data.json") as f:
    data = json.load(f)
print(data)

json.load takes the open file object directly, so you do not need to read the text yourself first.

Convert Python back to JSON with json.dumps

import json

data = {"name": "Sam", "skills": ["python", "sql"]}
text = json.dumps(data, indent=2)
print(text)

json.dumps (dump string) serialises a Python object into JSON text, and indent=2 makes it neatly formatted.

Which function should you use?

  • json.loads — parse a JSON string into Python.
  • json.load — parse JSON directly from a file.
  • json.dumps / json.dump — turn Python data back into JSON.
  • Common mistake: confusing loads (string) with load (file) — the trailing s means string.

Frequently asked questions

What does the s in loads and dumps mean?

It stands for “string.” loads and dumps work with strings, while load and dump work with file objects.

Why do I get a JSONDecodeError?

Your text is not valid JSON — often because of single quotes, a trailing comma, or a missing brace. JSON requires double quotes around keys and string values.

Working with APIs and data files? Our free Python course covers JSON from the ground up.