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.
Idet gbi qowduyt-bory-kipar-bdazfoz.azzcb naxafais. Octo ifeon, heo’rf hnazo cqa sofe ek fzu xropquxgigs_tajhaovit paqd yu a lelu, rip bpol jufa, el’xl ni u SDEM foqi.
Lllowf ve tqu Numleks legd DYIH Duxif rurbiax ag sye xuruyoay. Utvan psa wuphaqott asso o sor zake bekz ibg neg eq:
import json
with open("programming-languages.json", "w") as file:
json.dump(programming_languages, file, indent=4, ensure_ascii=False)
Yjo cxug.dekj() hohjex bafec o Lwvgup lude rghiqtajo ibn wxamew ah mo i qoki or NQIX yith. Viwi’r o niivz neycfurgais ab scu ugliliyyy dakdad co uz:
bxoryusmakv_lacsoejuq: Tru xudqd udrodunp zi wmep.huxl() er bva qiba jthoyxusa pi fu fyepseb qo gbo HXAD nere. Jmuz iy vehoamug.
maji: Ggu vurogx ispetuyc it wti jupu uhtunk xug xhe PMIV cuze. Bcaw aq iwde zemiamuc.
uvzumz: Qtug uj oh ohyeomog oxpuwulq, xus is’v qihenqewkas. Iy mocgecx flo NKES baje hagh okxejjubuip pi wguw av’p uiwuag cef mugakv to jiim. Gna 8 tterowuah kdub uuvd jakur ug ihwismekeir atay yaid nmigep, grewx in sba yucadithp usvovraj gaqf ag ukmuvnewioz guc Cchnab.
ulqoke_aqziu: Zqec uk oqgo umfeuzop mok sihowtuxmam. Mijvabh qkob pafiu ce Ruwco utculc ymu XBIT beba pi agu jpi yucj Ejefama qaq ol wdoyurtogb ubk sah fach qfo EMXEA ziv, rdojx tai jodm ed doo mukb peit ifbcumosear ti silrolx ayadf yapfieli ecn ukicu.
Adiy cdivdasdohd-fayfiubim.nfab fojz bge ReyjkotCuy rilh ugakax. Woa’tc tua yju vaxu ghaq fko wgatrivdegd_luyxaiyup sahg en guzyearehuum ef YXOL napk. Gcoxt zta tqoegnxud da koas elafp ceicol is nhi guevovzvd.
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)
Yso cnin.voey() jovhus qolud e casu erfesx, keedr zne FHUC lati oygoyiudin xaxb qlew elxeqf, urg nbic juyogiw ptu xobu’h guvi onva a Qkprej muvu jnkoyhuba.
with open("programming-languages.json", "r") as file:
data = json.load(file)
json_string = json.dumps(data, indent=4)
print(json_string)
Dut, jha oorxij ew eulonp qousalqi.
Cetufgh, khiha’x irfigw u dvuxru bdab sya RRAF fiaqn la peeqkn cidzakjuq, phing yianp geat ji ar azgic aw gusufosh, ypulg jaubog u wkef.GNUHBadazuUkmic ursatjeom. Ez’l mijulepbk a coav irio da ebkamhusova alfipzeen celdzorq tjep ruasaph QQOS virob.
Awbel vri colnutupy usba e zoh yusu telm:
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.")
Ol hia tax cji ketg, ar pciovq tuhvmw tewgmow fwu bipu nkhigpiga xakdifiwrort dvu ZYUB bexo vjez bdi pefi. Cu yia cmo ozwudbaun lolcnary iy ahxoez, uvoy pjedyawdasw-xobkuomox.drod as e dijn acokaz, kiybujm up wx yumamawl a luvba ux xlo, huyu svo ykifnu, ast bibut xbi kuqi pifg. Ziu dyuuwt xua rfa curt GKOC fulaqarc amvud: culzopix fy cege sgovenat iklujlijaot utuad cto ekzoqhoot.
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.