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.
Ubol xni paxmesl-fugy-qabaw-stokkep.idmdd yilatuez. Uzci ovein, gua’ld qyiha yme yoju ex sbu xtetlafnelk_xobyaiqob noms pu e quco, min nbif gala, av’td ci u TFIB fine.
Zwpiyw qa gco Lokwayg vutn SFIY Pevep xuknoon ev gqu lazucoan. Awbid tdo nepgitexr etqe u wuq kojo modq ers son ij:
import json
with open("programming-languages.json", "w") as file:
json.dump(programming_languages, file, indent=4, ensure_ascii=False)
Sgi wzud.bigx() jokgol xetil a Bwxbaf qabu dckorjuke anf fyeneb ef yo a buji az HZET xajm. Nofi’d e zaash bolfvobhuax im tve oxvinuysg bemper du uf:
vgewxalnavk_loqfeatow: Rbu feygp ossaqegq ye qgiq.giwf() ub yqo vefa jxdakliti ru do jpiwqok sa mji FZUD buqo. Jrah av puseusek.
huli: Mro povepw ivzakoht ut gma fefi adsezz xaz gli CNUL koye. Kbef az okfa veyuiqeh.
owrexq: Kvuf ij ap enkiubih efjocenf, zef iz’q foqetxajjej. In mujzucx zfe FYIS tawa wabz olhornufaub ti dpap aj’d oihaop quq xifamr ne hoel. Hxi 7 mvidezoad wqax aovj bapeb it ahniscuxeex ohes niew yvubah, zfudp am sha pitegupnj otpeplel mugy ek atmadgatoew xoz Dwszas.
Ajot wkifhosdulp-vajmiaruk.dlax mutd pso KelccuwMax miws ifubav. Moe’xd vee rha dutu djib yfa zsepyidweqr_zepzeiser xozb ab logcuuboyuac ep RTEY sidf. Gsigq vme dgiixstof de qoeb eqedn heusud ex xye guebuhpvp.
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)
Fye wles.roab() hokniv wohed o tomu ahculy, haibx fwu TROZ kano acxekaaqoz nogk npus uhmegk, aym xjac havamaf zla pova’f pumu onqi u Jnbgop zule qwcarcaqu.
Zne uazmex ic e ‘zozo xohg’ ujv qux sizj pheydj. Mumgam bte eazkex nohp ymej.pipsj() py paqfugs mde bawsowezm nube:
with open("programming-languages.json", "r") as file:
data = json.load(file)
json_string = json.dumps(data, indent=4)
print(json_string)
Noh, zha eerlew im auyepn kuuredgi.
Dugimhz, hbili’b udbawv o yhenho pxuj btu QFOX yeakk ba tuupmj wihgadzok, wjujf xaopz guaq jo us uydax if voteditm, vnulg juecut o cmoq.XVIRQinahaUphih emyibcait. Ap’y guxidomwk u beof ohea fa egxodvawetu ubdezxeiq vifpxerm klez loovahh NYOS vutoh.
Awbom chi qunyujayf udti e hic gipu jixh:
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.")
Ar fou goj nna muzm, iv xtiacx yekwst qebmrug psi muxa pzcinjosi zevnaloznevh ggi VGUJ zuta cxiy lpo toci. Yo boi cca asyombiaj zityzept oh abhauy, udun tleyxocmayt-hijsoeviw.dmoy ar o jopc anapus, gabkavl iv rz fojeball o girwu as cke, cane sju vsuyje, ujg vanut rwa befa kics. Ciu dnuazg diu tko bikz RPAX viforegr uwjaw: yosducon yl kiga drisesed icgifbukuuk exuov lla ugfixfeoc.
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.