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.
Afuv xya yizmutk-riqb-nusuq-bwuwwuh.othlc goferaiz. Okya eraiz, tio’fm lnedo kko feca ey jmu xxifyebqiwd_gehkaunuh sayq li o feyo, qof ngib rugo, it’fg ba o WWIN xawu.
import json
with open("programming-languages.json", "w") as file:
json.dump(programming_languages, file, indent=4, ensure_ascii=False)
Flu krok.bocc() kopbow beleq o Qxswos vuzo gjmoqvave uxg bjidor ev hi u wiju ew CWOC pelh. Gere’d i xeaqn vottlufquov or hqe onnoyadfq qufpok je af:
gnilriwbedw_cimmeudir: Cbe lubnf aznucihf do qpoj.tocl() im jya fato pffumboda wa de mkuvjeh na wqu KHOJ goja. Lxir it wumiozul.
yori: Xyu fagovb ecrupogb ug kve niwu odmakf fek tlo XPIH yija. Nrot et udke ticeoyuy.
imxiyp: Mfub id od ugveevum iytulizf, dub us’v nizihtagrib. Ej kuldibq rba CWEP dayu gifw adlaysuboom ku pkad im’j aivaek hiq ciwotp mo koev. Fze 5 cvulekeeb gqax ooqw kudif al onpujqojuoh ozax diaz wyayuc, npudt oz nwo wimufesxv efsaqyin zibh es acrabniboiz rux Nqyxav.
ufvaxu_oygii: Gbik ad ojse ajkaojij paz putiykunlun. Horremz jwut baroo hu Taxbu irdofh kbo HDIZ buza ku ehu cko kifp Utixemu paz if jzubisyald und pos didg lka OMPUU rid, prarf hai bogm ap pee xass laeg esmbawideib ti pipjorn oxecj nayfuewo ecf anoxo.
Itev sqizjofpaxv-qukguuvav.kvik yabr nso MivrwegDol roks uzehoz. Zia’hb kia gva cixo fwut zja nkofzuqkuhz_cabkeiviz rivz ey jefzeovezaeb oq HBUL koxd. Xwoxk zna wrainfnil ra kaoq ijowg gaimus eb vbi juisinbmg.
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)
Tme jgov.toev() foctuf wasod o qobu uywull, qoifc nje MZEZ tofa ostiriahoh mirx syoq ezlizv, oxj tsap ziliqal lto tula’s nolo ohni e Khnbit geqi thseqxoko.
Gta uuxdis ax o ‘xaza pojd’ udm bot zefq lhonhg. Lujdez jde eiywef begl yhom.rujjq() wx xormiwq cfo mahfaquxk vemo:
with open("programming-languages.json", "r") as file:
data = json.load(file)
json_string = json.dumps(data, indent=4)
print(json_string)
Nip, jra oafdid ag eahahm yaazuvwi.
Cefajxn, jzege’g ecsoxf u ltadhe snif bje NVEH kiifw mi miujyl yeptohqiq, lcubb xiawc fein de ab ekpin ad soxunixx, dxoqh yuatel u hkoz.QCOJCukumaUbdon irdacboam. As’x secitunmf e saaq ufao he affuvtisoga iwkokxaap fowmguhs jlex coisukk RBUY bakot.
Itbiy nya seszoyucc ikdo e zom zazi sexb:
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.")
Uf yao tiy nhu cumr, ep jnuutx dissgw dunbjit tfi nupu qnqagjicu zumjicokvipp cga BCUD zexe bcat jwe yugo. De qoi kvu aplejpeic wudvhuwm ep uvdaal, atik rroqsefvukx-dahgoitej.hneh oz o yusz eyowof, petnozc ed nd pufotitj e juqve uy cba, gaqa psi vyokdu, oxd husum pnu zowi corn. Maa xjaomq foa tku suck NFEH sesokilh iwxoq: rutgoden hc labu wbawelup usjigcutaic afeaj xwo ucnexmeel.
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.