read_json और to_json
JSON web APIs की भाषा है, और Pandas इसे सीधे पढ़ और लिख सकता है।
In this page:
Syntax
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
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. #}
आम गलतियां
- गलत orient चुनना
- गहराई से nested JSON को flatten न करना
- 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: