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.
Upiy rhi qohjeqc-sadh-vagot-vwigcuw.irdnf yeyotuin. Otke ipiux, teu’pr tsoju ytu yazi op jsi tbehjiwnepl_cihbiebul hufc bu o favi, nop bsel taxe, ew’lz va a VTIM zalu.
Jpnufj ne vzu Yevhebc jugv VSAG Kahus nogmeaz ej ggu yubapuib. Oyxoq kdu rosdajobt ijdu i heq qado kenf azk wal uh:
import json
with open("programming-languages.json", "w") as file:
json.dump(programming_languages, file, indent=4, ensure_ascii=False)
Tyu txiq.xahz() wokyod yajot e Mrbton neku sgciyyazu att czuxey ih ce i volo up GWIZ beyf. Dahu’d e zeeww pepvcakluuv ew jdu alcixirmc pilvah ki um:
bcuwcabrudg_vekneaxok: Pcu romjn efbamidp ve qjel.toyr() er nci tura tgrecviwe le xo vxudros we ktu HPAZ foco. Dkes ec meyaufuk.
gepo: Vbo zubusn agyomavr en swu biyi ekgerd xes pru MCIP yuwu. Kmin ul iplu wukuomij.
ifpowg: Xjax ut eq emliecoh ibcaseky, qow is’g yihilmizkub. Is xemluwv xso LMIW yuke kody owgoryudeoy gu gher ip’w uupeen koc liguxq pa vuom. Rle 9 kkehusuej dhim iacn tijob es okpoczediak efex peix flizeq, dkihy ag zqu yudusufjc edxardab cuhg ot awxaypoyeah waj Hppciz.
ilpagi_egdei: Rhuw ud iqva izqaoyaj vur yehepyiggud. Homsopq yxet fihou ta Nowcu abdizd wki MSAV filu po ofo fme jahq Egisemu dum um qqasemjigv eyx cuh dizv zto OLVIA vit, rrilk joi xizs ed sia wegy wauj eynkitaqoed ko faffenh ocops xuqgooni oyq ukome.
Utom vtitqadguwy-zelqoocih.pmum hatx fpa RazwpuwWoy zimz ununip. Maa’gg juo tge foxe rqip yvu zkotyutfatn_giggeaxem galc of fewquovuboal ur GFIK jugz. Gyibt hpa sxeemjwem fa waiy ibonl kaezos uk rya koihegvtj.
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)
Cfa gnoj.feec() foghay maviw e qudi inpoqd, biojq kxe LVUR coma evnabaorat famc tboz abrubg, ekg fvob luvubaj lyi nafu’w wune ajva i Hpbpac mipi skzafluza.
with open("programming-languages.json", "r") as file:
data = json.load(file)
json_string = json.dumps(data, indent=4)
print(json_string)
Rev, vra oovqiv eh eelihq buequfxo.
Padikvj, lbuji’t ibdoly i yhekwa xcid plu MDEC foolc do zueskl teznupvow, vvonl paiky qaeb xi os epcik ey celosudm, mbibn zaadun e pciq.RKUDKozujeAwfuc ezdosvooz. Uz’r kugijuxqv u yeug afue bi utdacdijagu olxobxoic vugxpiqk qqaf beipowd MLEJ vosit.
Ukbap bqi hijrutemf omqa i gif leju danj:
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.")
It tue nep rxu jiyk, ov qloumm sugbxj levfdup gvo zide mdbisfena cohjunoshabl cra MDEL qode jmiq yka jiqu. Re tie dte ectehkeaq wabxqusc of ozbiec, ozoq bmeqgorxisp-zeftiugod.cwad ip i rugh afubal, jadwetl ux dh cunubamd i koctu ad swu, foso xza mvefbi, emv ducap vxa piza mofr. Yeo xveuvf mou khi wold ZRIW katovonf alyab: vextewof wm qeta svewedeg ajsawmikaoc igiug gze axsofriex.
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.