El módulo json nos permite gestionar ficheros con formato JSON (JavaScript Object Notation).

La correspondecia entre JSON y Python la podemos resumir en la siguiente tabla:

JSONPython
objectdict
arraylist
stringstr
number (int)int
number (real)float
trueTrue
falseFalse
nullNone

Leer ficheros json

Desde una cadena de caracteres:

>>> import json
>>> datos_json='{"nombre":"carlos","edad":23}'
>>> datos = json.loads(datos_json)
>>> type(datos)
<class 'dict'>
>>> print(datos)
{'nombre': 'carlos', 'edad': 23}

Desde un fichero:

>>> with open("ejemplo1.json") as fichero:
...   datos=json.load(fichero)
>>> type(datos)
<class 'dict'>
>>> datos
{'bookstore': {'book': [{'_category': 'COOKING', 'price': '30.00', 'author': 'Giada De Laurentiis', 'title': {'__text': 'Everyday Italian', '_lang': 'en'}, 'year': '2005'}, {'_category': 'CHILDREN', 'price': '29.99', 'author': 'J K. Rowling', 'title': {'__text': 'Harry Potter', '_lang': 'en'}, 'year': '2005'}, {'_category': 'WEB', 'price': '49.99', 'author': ['James McGovern', 'Per Bothner', 'Kurt Cagle', 'James Linn', 'Vaidyanathan Nagarajan'], 'title': {'__text': 'XQuery Kick Start', '_lang': 'en'}, 'year': '2003'}, {'_category': 'WEB', 'price': '39.95', 'author': 'Erik T. Ray', 'title': {'__text': 'Learning XML', '_lang': 'en'}, 'year': '2003'}]}}

Escribir ficheros json

>>> datos = {'isCat': True, 'miceCaught': 0, 'name': 'Zophie','felineIQ': None}
>>> fichero = open("ejemplo2.json","w")
>>> json.dump(datos,fichero)
>>> fichero.close()

cat ejemplo2.json 
{"miceCaught": 0, "name": "Zophie", "felineIQ": null, "isCat": true}

Updated: