En este artículo, discutiremos cómo convertir una lista de diccionarios a JSON en python
Método 1: Usar json.dumps()
Esta función convertirá una lista de diccionarios a JSON.
Sintaxis:
json.dumps(dict, indent)
Parámetros:
- diccionario: nombre de un diccionario que debe convertirse en un objeto JSON.
- sangría – define el número de unidades para la sangría
Ejemplo: programa Python para crear una lista de diccionarios de datos de empleados y convertir a JSON
Python3
# import json module import json # list of dictionaries of employee data data = [{"id": ("1", "2", "3"), "name": ("bhanu", "sivanagulu"), "department": ("HR", "IT")}, {"id": ("4", "5", "6"), "name": ("sai", "poori"), "department": ("HR", "IT")}, {"id": ("7", "8", "9"), "name": ("teja", "gowtam"), "department": ("finance", "IT")}, {"id": ("10", "11", "12"), "name": ("sai", "jyothi"), "department": ("business", "IT")}, {"id": ("13", "14", "15"), "name": ("prudhvi", "nagendram"), "department": ("business", "IT")}] # convert into json final = json.dumps(data, indent=2) # display print(final)
Salida :
[ { "id": [ "1", "2", "3" ], "name": [ "bhanu", "sivanagulu" ], "department": [ "HR", "IT" ] }, { "id": [ "4", "5", "6" ], "name": [ "sai", "poori" ], "department": [ "HR", "IT" ] }, { "id": [ "7", "8", "9" ], "name": [ "teja", "gowtam" ], "department": [ "finance", "IT" ] }, { "id": [ "10", "11", "12" ], "name": [ "sai", "jyothi" ], "department": [ "business", "IT" ] }, { "id": [ "13", "14", "15" ], "name": [ "prudhvi", "nagendram" ], "department": [ "business", "IT" ] } ]
Método 2: Usar json.dump()
Esto escribirá los datos JSON convertidos en un archivo.
Sintaxis:
json.dump(dict, file_pointer)
Parámetros:
- diccionario: el nombre del diccionario que debe convertirse en objeto JSON.
- Puntero de archivo: puntero del archivo abierto en modo escribir o agregar.
Sintaxis:
with open("mydata.json", "w") as final: json.dump(data, final)
donde mydata es el nuevo archivo JSON. Finalmente, tenemos que descargar el archivo JSON creado.
Sintaxis:
files.download('mydata.json')
Ejemplo:
Python3
# import json module from google.colab import files import json # list of dictionaries of employee data data = [{"id": ("1", "2", "3"), "name": ("bhanu", "sivanagulu"), "department": ("HR", "IT")}, {"id": ("4", "5", "6"), "name": ("sai", "poori"), "department": ("HR", "IT")}, {"id": ("7", "8", "9"), "name": ("teja", "gowtam"), "department": ("finance", "IT")}, {"id": ("10", "11", "12"), "name": ("sai", "jyothi"), "department": ("business", "IT")}, {"id": ("13", "14", "15"), "name": ("prudhvi", "nagendram"), "department": ("business", "IT")}] # convert into json # file name is mydata with open("mydata.json", "w") as final: json.dump(data, final) # download the json file files.download('mydata.json')
Producción:
[{“id”: [“1”, “2”, “3”], “nombre”: [“bhanu”, “sivanagulu”], “departamento”: [“HR”, “IT”]}, { “id”: [“4”, “5”, “6”], “nombre”: [“sai”, “poori”], “departamento”: [“HR”, “IT”]}, {“id ”: [“7”, “8”, “9”], “nombre”: [“teja”, “gowtam”], “departamento”: [“finanzas”, “TI”]}, {“id”: [“10”, “11”, “12”], “nombre”: [“sai”, “jyothi”], “departamento”: [“negocio”, “TI”]}, {“id”: [“ 13”, “14”, “15”], “nombre”: [“prudhvi”, “nagendram”], “departamento”: [“negocio”, “TI”]}]
Publicación traducida automáticamente
Artículo escrito por bhanusivanagulug y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA