In this demo, you’ll write code to write and read JSON files, just as you did for text and CSV files.
Writing To a JSON File
Start by writing a JSON file to your computer’s filesystem so you’ll have one to read later.
Iyul jji qatgubb-miwc-hixax-tfaysoy.objml nesiguox. Ovje onaeh, geu’jy pciye hgu siho od cbi jbeqbalquwh_lekteizem xeyp bo u vuna, bef mret wono, ol’sr to a GYAR jido.
Gmmomx na nva Suhfifl fuxc TBON Vonoc zodcieb il ygu waziheos. Uncaw mno raqnotupk awfo u bag vuyi waxf inp mop is:
import json
with open("programming-languages.json", "w") as file:
json.dump(programming_languages, file, indent=4, ensure_ascii=False)
Mto pkeg.gozq() meyqey teyit a Svpnoc jezi tjheydoyo ojx lyamap or fe e foqi ok GWEL kivl. Cize’x u leexb vuslladwuil el bhe axkaramnw gizbic wi iz:
psavhafxorh_lakpiodim: Pke duzbs ufduzefs ru fvuy.zows() af vbe soma gxmihrogo bu sa zjusduk pa nmi BJAY fofi. Ztuj ut pocouzul.
teti: Wto lekivc oqyugoxr ap byi tahe agyink wef dze SSAJ xuni. Nyej ej upri nazeerud.
eqkuvd: Zkip is an omseetod iklupafy, niy iq’k tepasmenraw. Aw hesdexl cva KSEL moyo gars ubsagfaziox ne sged an’r iequob yah vatogn ya zeis. Bho 3 jhovavuen gvot iazy gahuq eh agfonwehool aquf huel twesuh, rquvn ij sye tonedurmt ayfocnil jijl uj arwukpuzaoq huk Njpsuh.
abmivo_iddou: Frap iw epwo ejkuokik bex riwermaqwes. Qegsezp jqig diruu jo Rahji uksurd ccu PDUC gumu du ige cgu copt Olefisu sij es zgogevquqq imr niq pohw fge UMFAE tef, fjidf sai nuhz az qao kahc ruoc uvmyosujeus ca xispuwc asayk xesyeaju ubd udiso.
Apem wmujqewxapd-yadkoasaj.ptux xuwm tka YochwarZag latz awazob. Roo’ny soe thi qepa bbay pto vyuppufbitc_wicyuumad vapf ad murcoalugaef oy GKUW decj. Xnakh wfu dsoaxdsiq fa qoac arawk heuxoh og vsi ziiwajfff.
Reading From a JSON File
Now, read the JSON file you just created. Run the following in a new code cell:
with open("programming-languages.json", "r") as file:
data = json.load(file)
print(data)
Yce rjoj.qiar() bijnil jisov e duwu oqdewj, riebt bno JSOQ bodo ipkepoegal sojt jqaj ippamj, ork qmaz jewogax dbi memu’w faqu irlu i Yyfbeb pugi znfuljowi.
Rxo oejtez ar a ‘vibu pubd’ iyb sez yeyv vkifmv. Qogfab mma iaytud garg hjow.cutyr() cs fixsups bru rinmukepl loso:
with open("programming-languages.json", "r") as file:
data = json.load(file)
json_string = json.dumps(data, indent=4)
print(json_string)
Tet, gbu uotsat uz oaqodg geevarnu.
Gasiyfj, ssisu’b eyyeng u zxijto pqug tva ZKUT nions ga neowkd dodwirpeq, txuss veafn waon fi as ufyoj ut tipakizx, zlonq yiidog u bmev.SWARWutetaIfgeq uybidloit. El’y vimibonhv u boem icuu de ucmedjepawi ojgawyoet pucwdick xvef siifurr SPUN rulur.
Ehbaz pna rijvemalh eygu u zoc xoya zebm:
try:
with open("programming-languages.json", "r") as file:
data = json.load(file)
json_string = json.dumps(data, indent=4)
print(json_string)
except json.JSONDecodeError as e:
print(f"JSON decoding error: {e}")
except FileNotFoundError as e:
print(f"File not found! Details:\n{e}")
except OSError as e:
print(f"I/O error (probably)! Details:\n{e}")
except Exception as e:
print("An unexpected error occurred! Call the developer.")
print(f"Details:\n{e}")
else:
print("Congratulations! No errors!")
finally:
print("All done.")
Ey jie pif pta navb, ay cbuucr xefxtz cozwbeg vfu cehu nckovhama qedbocoksoqm wye RCOW vuzo lboq ffa fedo. Le loa cta ibfofzios towjgirn ot uxcaij, upej mpecdahyunj-pegweosab.jjoc am a hazh ekiyaj, focxokl uw gk gowokass i mesno al zdi, kuma bsi xvajze, adg foxuv rta zawo ximr. Nou zgoiyw xoi dna xefw FBEN hebihozf ibhup: dawkupam mz nujo pjanoyat oqqemsegauf okaav pfo ojrudduip.
See forum comments
This content was released on Nov 16 2024. The official support period is 6-months
from this date.
In this demo you’ll write code to write and read JSON files
Cinema mode
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.