JSON y datos
El formato que hace que un programa en Python y una app en el celular se entiendan.
JSON and data
The format that lets a Python program and a phone app understand each other.
Ya sabes que un diccionario de Python guarda datos con nombre. El problema es que ese diccionario solo existe mientras tu programa corre: para guardarlo en un archivo, mandarlo a internet, o que lo lea un programa escrito en otro lenguaje, necesitas convertirlo a texto plano con una estructura acordada. Eso es JSON.
Fíjate que un JSON se ve casi idéntico a un diccionario de Python. No es casualidad: Python copió esa sintaxis de JavaScript, y JSON nació justamente para ser ese "casi igual" que cualquier lenguaje puede leer.
You already know a Python dictionary stores data by name. The problem is that dictionary only exists while your program is running: to save it to a file, send it over the internet, or have it read by a program written in another language, you need to convert it to plain text with an agreed structure. That is JSON.
Notice a JSON looks almost identical to a Python dictionary. That is no coincidence: Python copied that syntax from JavaScript, and JSON was born precisely to be that "almost the same" any language can read.
El módulo json viene incluido en Python. dumps (de "dump string")
convierte un diccionario o lista en un texto JSON.
The json module comes built into Python. dumps (from "dump
string") converts a dictionary or list into JSON text.
loads hace lo contrario: toma un texto JSON —el que llega de un archivo, de
internet, o donde sea— y lo convierte de vuelta en un diccionario de Python normal, con el que
ya sabes trabajar.
loads does the opposite: it takes JSON text — arriving from a file, the
internet, or anywhere — and turns it back into a regular Python dictionary you already know
how to work with.
Juntando esto con open(), que ya viste en el tutorial de archivos, puedes
guardar el progreso de un programa y recuperarlo la próxima vez que corra.
Combining this with open(), which you saw in the files lesson, you can save a
program's progress and get it back the next time it runs.
Nota el detalle: json.dump() (sin "s") escribe
directo a un archivo, y json.dumps() (con "s") devuelve un texto. Lo mismo para
load / loads. Es fácil confundirlos al principio.
Esta misma idea es lo que hay detrás de cuando una página web pide datos a un servidor: el servidor le manda JSON, y el navegador lo convierte de vuelta a algo con lo que trabajar —ahí está la conexión con las APIs de las que seguro has oído hablar.
Notice the detail: json.dump() (no "s") writes
straight to a file, and json.dumps() (with an "s") returns a text string. Same
for load / loads. Easy to mix up at first.
This same idea is what sits behind a web page asking a server for data: the server sends JSON back, and the browser turns it into something to work with — that is the connection to the APIs you have surely heard about.
Convertir datos de Python a JSON y de vuelta, guardar el progreso de un programa en un archivo, y por qué este formato es el idioma común de casi todo internet.
Ahora, algo que todo programa necesita tarde o temprano: manejar fechas y el paso del tiempo.
Converting Python data to JSON and back, saving a program's progress to a file, and why this format is the common language of almost the entire internet.
Now, something every program needs sooner or later: handling dates and the passage of time.