Pandas Read JSON

Pandas Tutorial

Pandas read_json() — Beginner to Advanced Guide

read_json() is used to load JSON data into a Pandas DataFrame.

JSON is super common in:

  • APIs

  • Web apps

  • Config files

  • Logs


 1. Basic Syntax (Beginner)


 

  •  Reads a JSON file
  • Converts it into a DataFrame automatically

2. Read JSON from a String


 

  •  Useful when JSON comes from API response or variable

 3. Read JSON from URL (API Data)

  •  Direct API → DataFrame
  •  Very common in data science projects

4. JSON Orient (VERY IMPORTANT)

JSON can have different structures.
orient tells Pandas how data is arranged.

Common orient values:

orientStructure
recordslist of dicts
indexdict of dicts
columnscolumn-wise
valuesvalues only
tableschema-based

orient='records' (Most Common)

JSON:


orient='index'


 


 5. Nested JSON (Intermediate Level)

Example JSON:

read_json() alone is NOT enough

 

  •  Flattens nested JSON
  •  Essential for API responses

 6. Read JSON Lines (.jsonl) (Advanced)

Used in logs & big data

Correct way:

  •  Without lines=True → ERROR

 7. Select Specific Keys from JSON


 

  •  Same as column selection in CSV

 8. Handle Missing Values


 

or


 9. Convert JSON Column to Separate Columns

Useful when:

"address": {"city": "Delhi", "pin": 110001}

10. Read Large JSON Files (Performance Tip)


 

  •  Saves memory
  •  Used in production systems

 11. Common Errors & Fixes

 ValueError: Trailing data

 Use:


 JSONDecodeError

 Check:

  • Invalid commas

  • Missing brackets

  • Wrong quotes


 12. read_json() vs json.load()

Featureread_jsonjson.load
Direct DataFrameYesNo
Nested controlNoYes
API readyYesNo

Best Practice

  • Simple JSON → read_json()

  • Complex nested JSON → json.load() + json_normalize()


 13. Interview Questions (Favorite)

Q1: Can Pandas read API JSON directly?
 Yes, using URL

Q2: How to read JSON logs?
lines=True

Q3: How to flatten nested JSON?
json_normalize()


 14. Real-World Example (API → Clean Data)


 


 Summary Cheat Sheet


 

You may also like...