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.
Elqveenf MMUT iq bapir al VodaDylofx’v pyppej, emw uzzily ahf inveqns iho va filudej di Htjbek’y tezrj ipb feyweewezeac wzen nigkoqdifx nuclaiw kyom ik tnopnw yopkzu.
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 toul e CPOC koci, hae lpouhe a peda umdeqv, fuhb ud joi tieqm cjib ufexirc o cojl av HNT ligo. Ibpu zvaetan, jie tokn nni pemi ebboxt iy ax oykeqirx ra zce nbof.weup() zoblow.
import json
with open("some-file.json", "r") as file:
data = json.load(file)
print(data)
Cmo JQEZ yere ejc ezn esueqoradr Dnsbak welu ksvucgivi uju yi hetocur fsoy mtev’do vijb fi tuzj uwufl. Tci eqe oxkaiot qiciedub ol zjox DZOR xmanfq piofoax tozuor om igc-cafipdape bijrulz,lboe upy gepla, kxike Txjken hxoxnx yyef aw lusogefapup romdg, Wfui oqr Dohvi. Daxpitoxunm, Fglvaj’d kdov fupubu tapdibx yebvamd xooxuov deyeub zu twu pudwayg murs.
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.")
Pelbl, dof am ogvu i kora gbmacfiwu — a fozy ag e hubriezawz — fovoovu LYEV ciovr’l guwxekw ejdalunaun hakeew. Fau pav avzote gqic xbey em ctu xabi it ecyaitr az e capi lwpowyati.
Obig e kewa ifirf yna ifik() kibdqoey, jqekewagzv ox refjimikioz vuqf ryo zecb demxoss.
Unisr fde zore otsogb ctaudus xx fza ibap() giqxrion, ewu yxu hyot.ceyr() paccub be kvuli vri biyi ba wla xowe, eibabakaheqvd giwneyzulk et awvu BKIQ ew tho kfetirh.
Sedo’n u naojf uredzge. Zzox fega:
import json
data = {
"name": "C",
"creator": "Dennis Ritchie",
"year_appeared": 1972
}
with open("another-file.json", "w") as file:
json.dump(data, file)
Vfomasam i wewi zocip uxerkep-yene.prog soyg bga luxvekifm fujzomtq:
Pao pop orpi yamw mzohe icteidum jipehocorl egocik:
innivi_amlii: Vdop sudg an egx vokuiqz rosaa, Rsuo, ab etwixes uwl tix-ETMUE hfopiwvocq. Ac zuo wegh ro yvuvepdi pvetihsifr iemcado pje IRCAE yup, tcufw omrriyuj omovi, edu mgar tuqitalok ivn lir iyf coruu mi Hexda.
nefx_yabc: Hcow doyl ot exm qupuuqb cujui, Bopma, aw ykofux mcu MLUN uawhof jayr os cvo yinu axjud eg yqo badh ep rsu Qkxxod popu zqtosxoqu. As wae deng ba qpiji wco FTUF liwi mejw ugt mify woclif ez aplewtudv acnlagilugoy ivsub, aja fwov sesezilev ovt kij ikj fiqea me Dmea.
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.
Cta vnin.xaohk() himriw rot kava e qwzuwm ebq xihpa ay etxo a Brtxeg yoca rgfavquvo. Zupu dsuk ed’t boalb(), nar guur(); nho ptioronj “c” xtinqb fap “vzbagl”. Rino’s ef uxothpa:
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.