</>
Skip to content
Pandas lessons (9/42)

Pandas — Read JSON

Read JSON

import pandas as pd

# From file
df = pd.read_json('data.json')

# From string
json_str = '[{"name": "Alice", "age": 25}, {"name": "Bob", "age": 30}]'
df = pd.read_json(json_str)

Write JSON

df.to_json('output.json')
df.to_json('output.json', orient='records')

Nested JSON

import json

with open('nested.json') as f:
    data = json.load(f)

df = pd.json_normalize(data, record_path='items')

JSON options

# Different orientations
df.to_json(orient='records')   # [{col: val}, ...]
df.to_json(orient='columns')   # {col: {idx: val}}
df.to_json(orient='index')     # {idx: {col: val}}

Mini Practice

  1. Read JSON file
  2. Write JSON file
  3. Handle nested JSON
  4. Use different orientations

Up Next

Continue with Excel - Working with Excel files.

Related Topics

Frequently Asked Questions about Read JSON

What is Read JSON in Pandas?

Read JSON is a fundamental concept in Pandas. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Read JSON?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Read JSON.

Why is Read JSON important in Pandas?

Read JSON is essential for Pandas development. Understanding this concept will help you write better code and solve real-world problems more effectively.