Syntaxe
{
"nom": "Léa",
"age": 34,
"abonne": true,
"tags": ["photo", "rando"],
"adresse": { "ville": "Lyon", "cp": "69003" },
"telephone": null
}Règles : guillemets doubles, pas de virgule finale, pas de commentaires. Valider : python -m json.tool fichier.json.
Python
import json
data = json.load(open("contact.json", encoding="utf-8"))
texte = json.dumps(data, ensure_ascii=False, indent=2)
json.dump(data, open("copie.json", "w", encoding="utf-8"), ensure_ascii=False, indent=2)
JavaScript
const data = JSON.parse(texte);
const texte2 = JSON.stringify(data, null, 2);
const reponse = await fetch("/api/contact"); const obj = await reponse.json();Voir appeler une API.