Python JSON to Dictionary Example

If you have JSON text and want to turn it into normal Python data, the usual tool is the built-in json module.

This example shows one practical task: converting a JSON string into a Python dictionary so you can read values by key and use them in your program.

Quick example

import json

json_text = '{"name": "Alice", "age": 25, "is_admin": false}'
data = json.loads(json_text)

print(data)
print(type(data))
print(data["name"])

Output:

{'name': 'Alice', 'age': 25, 'is_admin': False}
<class 'dict'>
Alice

Use json.loads() for a JSON string. If the JSON root value is an object, it returns a Python dictionary.

What this example does

  • Shows how to convert a JSON string into a Python dictionary
  • Uses the built-in json module
  • Explains the difference between JSON text and Python data

When to use this

Use this approach in common situations like these:

  • When you get JSON from an API
  • When you read JSON from a file or response body
  • When you need to access values by key in Python

If you want a broader walkthrough, see how to parse JSON in Python.

Basic example with json.loads()

To convert JSON text into Python data:

  1. Import json
  2. Store the JSON in a string
  3. Call json.loads(json_text)
  4. Read values from the result
import json

json_text = '{"name": "Alice", "age": 25, "city": "London"}'
data = json.loads(json_text)

print(data)
print(data["name"])
print(data.get("age"))

Output:

{'name': 'Alice', 'age': 25, 'city': 'London'}
Alice
25

How it works

  • json_text is plain text in JSON format
  • json.loads(json_text) parses that text
  • Because the top-level JSON value is an object, the result is a Python dict

If you want more detail on this function, read json.loads() explained.

What the result looks like

JSON does not always become a dictionary. It depends on the JSON value you parse.

Here is the usual mapping:

  • JSON objects become Python dictionaries
  • JSON arrays become Python lists
  • JSON true, false, null become True, False, None

Example:

import json

json_text = '{"user": "Alice", "scores": [10, 20, 30], "active": true, "nickname": null}'
data = json.loads(json_text)

print(data)
print(type(data))
print(type(data["scores"]))
print(data["active"])
print(data["nickname"])

Output:

{'user': 'Alice', 'scores': [10, 20, 30], 'active': True, 'nickname': None}
<class 'dict'>
<class 'list'>
True
None

Example: access dictionary values

Once the JSON has been converted, you can use it like a normal dictionary.

import json

json_text = '{"name": "Alice", "age": 25}'
data = json.loads(json_text)

print(data["name"])
print(data.get("name"))
print(type(data))

Output:

Alice
Alice
<class 'dict'>

data["name"] vs data.get("name")

  • data["name"] gets the value for the key "name"
  • data.get("name") also gets the value, but is safer if the key might be missing

Example:

import json

json_text = '{"name": "Alice"}'
data = json.loads(json_text)

print(data.get("age"))

Output:

None

If you use data["age"] and the key does not exist, Python raises an error. See how to fix KeyError when accessing dictionary values.

Example: nested JSON

JSON often contains objects inside objects, or lists inside objects.

import json

json_text = """
{
    "user": {
        "name": "Alice",
        "contact": {
            "email": "alice@example.com"
        }
    },
    "skills": ["Python", "SQL"]
}
"""

data = json.loads(json_text)

print(data["user"]["name"])
print(data["user"]["contact"]["email"])
print(data["skills"][0])

Output:

Alice
alice@example.com
Python

What to notice

  • Nested JSON objects become nested dictionaries
  • JSON arrays become Python lists
  • You access nested values one step at a time

For example:

  • data["user"] gives a dictionary
  • data["user"]["contact"] gives another dictionary
  • data["skills"] gives a list

JSON string vs JSON file

This is a common beginner confusion.

  • Use json.loads() for a string
  • Use json.load() for a file object
  • Do not mix the two functions

JSON string example

import json

json_text = '{"name": "Alice"}'
data = json.loads(json_text)

print(data)

JSON file example

Suppose data.json contains:

{"name": "Alice"}

Then you would read it like this:

import json

with open("data.json", "r") as file:
    data = json.load(file)

print(data)

If you want to learn that version in more detail, see json.load() explained and Python JSON module overview.

Common beginner mistakes

These are some of the most common reasons JSON conversion fails.

  • Using single quotes inside JSON text
  • Forgetting to import json
  • Using json.load() on a plain string
  • Expecting every JSON value to become a dictionary

1. Using single quotes in JSON

This is invalid JSON:

import json

json_text = "{'name': 'Alice'}"
data = json.loads(json_text)

JSON requires double quotes around keys and string values.

Correct version:

import json

json_text = '{"name": "Alice"}'
data = json.loads(json_text)

print(data)

2. Parsing Python dictionary syntax as JSON

Python dictionary syntax looks similar to JSON, but they are not the same thing.

This is Python data:

data = {"name": "Alice", "is_admin": False}

This is JSON text:

json_text = '{"name": "Alice", "is_admin": false}'

Notice the difference:

  • Python uses False
  • JSON uses false

3. Using the wrong function

This is wrong because json.load() expects a file object, not a string:

import json

json_text = '{"name": "Alice"}'
data = json.load(json_text)

Use json.loads() for strings instead.

4. Assuming the result is always a dictionary

If the top-level JSON value is an array, the result will be a list:

import json

json_text = '["apple", "banana", "orange"]'
data = json.loads(json_text)

print(data)
print(type(data))

Output:

['apple', 'banana', 'orange']
<class 'list'>

FAQ

What function converts JSON to a dictionary in Python?

Use json.loads() when you have JSON as a string. If the JSON root is an object, the result is a Python dictionary.

What is the difference between json.load() and json.loads()?

json.load() reads JSON from a file object. json.loads() reads JSON from a string.

Does JSON always become a dictionary?

No. A JSON object becomes a dictionary, but a JSON array becomes a list.

Why does my JSON conversion fail?

The most common reason is invalid JSON format, especially single quotes, missing commas, or trailing commas.

See also