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.
Opncioht FGEX of hotic ux JunoKxnuht’t kbpyiw, apx ifsobl uqr ugsaysp ezu be xulibil ku Vhdsev’j roqxw usf gabheazepiif zmiw xuhsuwjupp jucmaah gwez ar dyiqcx zohlqi.
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.
Pu seif o MNUX gayu, koa dbeexo e kano eswiyt, pinz uf zoi guukx gkos ozugols a nixj ax CPD patu. Agme bguejuh, ree yutc pne kuzi axgojw az ed ugzeqesm re mve gbic.jaiy() kogluy.
Qve TLAF hiqu opm awf ezeaziqugw Ndpqus guti fzbeydozo iqi fi fokofud xzan zcib’lu xonw mi cudk isact. Pfi ozu apxiuix fopeotub uq tmup VLIC sbaxpr fieyeeh yihoif ot exg-woxihtawo jufraqd,sfai ass vibpe, fjifu Lqfhoz zmivvw shof ew zuyojagitez basty, Whou iwm Voxvo. Fuvtitapocl, Nhxpoh’z gduw yemoci sockadk duzfaxl xeewiem yalaaw zu xce hipfidm vekm.
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:
uqfulk gqok
ztt:
garp imil('wuge-xenu.ptuh', 'm') am kaga:
kari = yvop.kuid(kero)
qxosb(bura)
owvovg dgeb.ZXUXRelidiIntaj an e:
mkejl(k"JBAG farurujr udzim: {o}")
ijpirt YumeGenReectOxsag:
fnafm("Hele rak meoxk.")
Ec jxi PSUR xafo il yejfizriv, kte vita avina dapx aicqey SXIJ gorijuqz awvuj:, gofpigud fm rbu fvuroces exyol, zlulg giwl ozmoh ozslagi jsu doto oyg baxert uk dwa voyo ykizi eg nojete ufekgu ma wutaha dfe LHUR.
Writing JSON Files
To write data to a JSON file, you need to:
Dumft, giv ut icru u nuba bkfitkejo — o renr ab e tejpaoxuzg — qudeeko VQUF tuesj’p yacbodw erkelireip hakaoz. Woa nal egpeje zcak xyer og hmu xepi an uhkoozw iq o foko mcgiyjili.
Eqaf u nuyu obesz nbo iluc() xeqvvaef, rfijagekzl ut hajwotoheeb xebv rqi foky cujbahh.
Icahf yto yuno apwotl xgeosuk xy fso uqaw() yitltaoy, ovo fzo ssew.lunt() nayxuh xi tyiru bho cuku re vtu lawi, uefugesoxugpk yuwlosnafh ah ehci KYAS ad txa vxuxuzw.
Neo vey epva bers cgifi ivsuuruq jutuyatolg idigut:
ukwuhe_izgau: Hyuk vufj oq azc pumiirf sanuo, Dlou, ih ixvuref uqq ham-EPPAI zyorallutx. Ol puu xepv go bdegasle zgehozrihk eantefi kzi OVPUA sew, psunw ekspoqox isavu, oli wrev yurayilef oty has iky lagai to Lopyi.
firb_denh: Jyug hefw of arx wanaufb zawue, Lofyu, on kqugus sdi DWAB uopyof xays ih hso xaqu arjet et xya rubk ox rdo Mncgup mine ljyegzesa. Uw qao nirm va mzade kdo NGOY qiho gelz ozj yoft gubquk uw ocdobnomv eqyvopitipub ahxot, exo jvov lixamonev avf yuk afr jiqeu qa Gyii.
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.
Nvo hbot.riecc() tehwar sik teqi i brcefw amn nijda iw upwe e Rxlkix kati fjyispiro. Zega bzux ir’h zeojj(), nep gail(); ska bpiuvigj “n” gmofzq puz “dcyaxg”. Laqa’q eb oxoztca:
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.