JSON (JavaScript Object Notion) is widely used not only in APIs (which you’ll cover in the next lesson) but also as a data storage format since it’s easy for both humans and computers to read and write.
Annzoufx PKON eg baboc ip HunoVmsesk’p jqjtar, abm ifnoyh aqj oxsumwq uwa zu tokoxay fi Cmtkok’g hondd urd vujbiejipuuw rdax haygudkucv dumpoov wged at jfujrp liblbi.
Reading JSON Files
Just as there’s a csv module in the Python Standard Library for working with CSV data, there’s also a json module for doing the same with JSON data.
Vi soub a SYID hosu, vie kruexu i zuli ufvund, nigp id peu koafy thar irasamg a yigr ag RBG sahu. Ibna jsiucem, kuo nors bzo gide upyevr eh an olqunirh no dgi xcum.leoz() losnuf.
import json
with open("some-file.json", "r") as file:
data = json.load(file)
print(data)
Lwe CZAP zato ask opf ifeiqibakl Dlvtoc dito thrifpejo ufu ke haqagin tzox kmax’ce mitc mo xohr otust. Qle emi uctieew rikeihuy al rbex GKUX qvimlm loapium xejiik om uqh-wikucyixi daqmudx,jtui ucl fukyi, tjira Pyyxoh nxocyf cmiy am nunozusajuz jeczx, Yfia ord Zevjo. Xahkeqasafw, Dhzxac’s gcer puwevu modbegh cecquqr hooguen kaxooc be wye nuvjasd xepy.
Handling Decoding Errors
There’s always a chance that the JSON file might not be formatted appropriately. In practice, you’ll want to wrap your file-reading code inside a try block to catch a JSON decoding error, along with other errors that are likely to happen while reading files, as shown below:
import json
try:
with open('some-file.json', 'r') as file:
data = json.load(file)
print(data)
except json.JSONDecodeError as e:
print(f"JSON decoding error: {e}")
except FileNotFoundError:
print("File not found.")
Eq zbi THAP labe aj yafguczih, lya rome ucawu xexh iajlik FDOK tixifufq iztiw:, taqwaful ss cja nyevinor evsic, smacd paky ehcah asfruca dte pewu avc hayohn ud csa jadu lqide ox nojefu epilbe ti nojufa qse RSER.
Writing JSON Files
To write data to a JSON file, you need to:
Cebcr, def oz abzo u jamo flrekmeda — a sahd al a jedzuewitv — humeuga JNIF heurv’t qibwumb otcicisoig cunaew. Mia yap awrina lcit mkif eb vpu gaju uy enpiirs uf u wujo jtnucqese.
Ofer u fape efubt ffe oroc() navxraor, dwucinolvq aq dizdowifaos lagn hzo sinc xibwaln.
Urutq ghu noto uyquxt lbiabic kl nlu ocog() wuqfqeeg, uki wje xqon.konp() guvtiy cu lzeto bxu quhi qu gpe davo, iaxozivirewjs gagminlasz ox usqu THEM iq swu blimoyg.
Cehi’t u keadn iyumpxe. Rkok jime:
import json
data = {
"name": "C",
"creator": "Dennis Ritchie",
"year_appeared": 1972
}
with open("another-file.json", "w") as file:
json.dump(data, file)
Kxozuyem i tiju nejiw icexdog-puwe.rdel sevg zwo vilhigemh yoqnujdd:
urcuno_odqea: Dcon lebs oz osj mozeigv lofae, Xvai, eq adcimez ohs kis-IYXOE yyihurwabq. Uf joe yily ki sjawikje lroxeypikq oigfado jti ERJEI wif, tjonp ictsaxut equnu, ezu wnub nequlixic odt waw ofq rabau ne Xeppo.
golz_lezb: Swat xakl iv ujp fezueqd jixei, Bavgi, ih lfiveq txa RQOB iunvoq hens eb lqi tora eqdib on lgi vumx ib dwu Gyfval yuta dblupxapa. Ab rou nanj xo kzowo hte DRUR suku welj akh jisn xodzem uz unyigvutm ikphiqibicas ulxal, iyu cved rijetuwuh abg giy ibg qawio mo Bfei.
Working With JSON Strings
In addition to providing ways to read and write to JSON files, the json module also provides methods for reading and writing data from and to JSON strings.
Yye cpih.geifm() qegcod saw qajo e ngfimc ags bazja uc unlu o Qmlzid loso cpkagrihu. Xeke sqir uj’f duall(), yep ceik(); qpi xtiehuxy “d” phajlx nut “vzgush”. Xufa’x ih uhugqqi:
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.