How to work with json using python ? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
7th Jun 2022, 5:35 PM
Mihir Lalwani
Mihir Lalwani - avatar
2 Réponses
+ 5
Here is another sample, it gets the json from an api on internet. import requests import json r = requests.get('https://moppenbot.nl/api/random/') #print(r.text) r_dict = json.loads(r.text) print(r_dict['joke']['joke'])
7th Jun 2022, 8:21 PM
Paul
Paul - avatar
+ 1
I usually do this whenever I need to read a json file: data = {} with open("file.json", "r") as f: try: data = json.load(f) except: print("an error happened") print(data) print(data["someKey"])
7th Jun 2022, 7:42 PM
Apollo-Roboto
Apollo-Roboto - avatar