Exercices Fonctions & Modules JSON serialisation
🎉

Bravo!

Intermédiaire 🧠 Fondamentaux 20 XP 0 personnes ont réussi

JSON serialisation

JSON est LE format d'échange de données sur le web. Chaque API que tu appelleras renvoie du JSON, et chaque fichier de configuration moderne est en JSON. Maîtriser json.dumps() et json.loads(), c'est indispensable.

json.dumps(objet) transforme un objet Python en chaîne JSON. json.loads(chaine) fait l'inverse. ensure_ascii=False garde les accents français.

Exemple :
import json
data = {'nom': 'Alice', 'age': 30}
texte = json.dumps(data)
objet = json.loads(texte)

Écris deux fonctions :
vers_json(data) qui convertit un objet Python en chaîne JSON
depuis_json(texte) qui convertit une chaîne JSON en objet Python

Exemple :
vers_json({'a': 1}) renvoie une chaîne JSON
depuis_json('{"a": 1}') renvoie {'a': 1}

Tests (2/4)

Serialiser
import json
result = vers_json({'nom': 'Alice', 'age': 30})
assert json.loads(result) == {'nom': 'Alice', 'age': 30}
Deserialiser
assert depuis_json('{"a": 1}') == {'a': 1}

+ 0 tests cachés

Indices (3 disponibles)

solution.py