← Back to Pandas Course | Chapter 11: File I/O | Lesson 3 of 7

read_json और to_json

JSON web APIs की भाषा है, और Pandas इसे सीधे पढ़ और लिख सकता है।

In this page:

  1. read_json/to_json
Syntax
python
df = pd.read_json("file.json")
df.to_json("out.json", orient="records")

read_json/to_json

read_json JSON text को DataFrame में parse करता है, और to_json एक लिखता है। orient argument layout control करता है: "records" row dicts की एक list देता है, "columns" column के अनुसार nest करता है। गहराई से nested JSON के लिए, pd.json_normalize उसे flatten करता है।

Note: orient="records" सामान्य API response shape से मेल खाता है।

उदाहरण: read_json/to_json

python
import io
import pandas as pd

js = '[{"name": "Ann", "age": 28}, {"name": "Bob", "age": 35}]'
df = pd.read_json(io.StringIO(js))
print(df)
print(df.to_json(orient="records"))
print(pd.json_normalize([{"a": 1, "b": {"c": 2}}]))

# Output:
#   name  age
# 0  Ann   28
# 1  Bob   35
# [{"name":"Ann","age":28},{"name":"Bob","age":35}]
#    a  b.c
# 0  1    2
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. गलत orient चुनना
  2. गहराई से nested JSON को flatten न करना
  3. JSON Lines files के लिए lines=True भूल जाना
चैप्टर सारांश
  • read_json और to_json JSON handle करते हैं
  • orient layout सेट करता है
  • records row dicts की list है
  • json_normalize nesting को flatten करता है
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.