Importación de archivos JSON — 8:25 min
8:25 min | Última modificación: Octubre 13, 2021 | YouTube
[1]:
#
# Carga de la librería
# ===============================================
#
import json
Introducción a JSON
[2]:
#
# Conversion a JSON de una lista
# ===============================================
#
some_list = [1, 1.0, "hola", True, False, None]
json.dumps(some_list)
[2]:
'[1, 1.0, "hola", true, false, null]'
[3]:
#
# Conversion a JSON de una tupla
# ===============================================
#
some_tuple = (1, "hola")
json.dumps(some_tuple)
[3]:
'[1, "hola"]'
[4]:
#
# Conversion a JSON de un diccionario
# ===============================================
#
some_dict = {
"key_0": 0,
"key_1": 1,
"key_2": 2,
"key_3": 3,
}
json.dumps(some_dict)
[4]:
'{"key_0": 0, "key_1": 1, "key_2": 2, "key_3": 3}'
[5]:
#
# Conversion a JSON de una estructura compleja
# ===============================================
#
some_data = [
{"Name": "Jameson R. Boyle", "Phone": "1-507-132-4340" },
{"Name": "Idona J. Singleton", "Phone": "1-662-859-5562"},
{"Name": "Ryder K. Maldonado", "Phone": "1-921-715-5315"},
]
json.dumps(some_data)
[5]:
'[{"Name": "Jameson R. Boyle", "Phone": "1-507-132-4340"}, {"Name": "Idona J. Singleton", "Phone": "1-662-859-5562"}, {"Name": "Ryder K. Maldonado", "Phone": "1-921-715-5315"}]'
[6]:
#
# Impresión de JSON con pprint
# ===============================================
#
from pprint import pprint
data_json = json.dumps(some_data)
pprint(data_json)
('[{"Name": "Jameson R. Boyle", "Phone": "1-507-132-4340"}, {"Name": "Idona J. '
'Singleton", "Phone": "1-662-859-5562"}, {"Name": "Ryder K. Maldonado", '
'"Phone": "1-921-715-5315"}]')
Ingestión de datos desde archivos locales
[7]:
%%writefile /tmp/data.json
{"Name":{"0":"Isabelle I. Dotson","1":"Erin F. Munoz","2":"Troy G. Harrell","3":"Yen W. Summers","4":"Jerome B. Carney","5":"Yuri O. Head","6":"Ima A. Richard","7":"Eleanor Q. Guerrero","8":"Stuart F. Daniels","9":"Cara G. Beach","10":"Nolan E. Cortez","11":"Darius H. Beach","12":"Christian T. Mercer","13":"Florence F. Gordon","14":"Wilma I. Patton","15":"Rajah I. Jensen","16":"Zoe U. Whitfield","17":"May K. Wood","18":"Beck Q. Monroe","19":"Amanda A. Marks","20":"Serina X. Lang","21":"Lionel L. Bartlett","22":"Florence Z. Oconnor","23":"Chloe Y. Sears","24":"Mariam N. Tyler","25":"Dana A. Jefferson","26":"Nola N. Chan","27":"Owen Z. Odom","28":"Serina B. Nash","29":"Salvador I. Powell"},"Address":{"0":"?","1":"P.O. Box 737, 6015 Ligula St.","2":"3706 Ut St.","3":"P.O. Box 939, 8016 Egestas Av.","4":"298-7004 Natoque St.","5":"P.O. Box 214, 231 Dapibus Ave","6":"Ap #496-7181 Ullamcorper, St.","7":"5425 Ornare St.","8":"719-8478 Nunc St.","9":"648-517 Velit. Av.","10":"Ap #766-6998 Gravida Rd.","11":"Ap #251-7151 Donec Av.","12":"P.O. Box 858, 1997 Elit, Avenue","13":"543-831 Est. Rd.","14":"P.O. Box 299, 7144 Orci Street","15":"Ap #782-6146 Turpis St.","16":"Ap #200-7243 Sit St.","17":"777-1067 Auctor, Ave","18":"P.O. Box 123, 9085 Lorem, Rd.","19":"638-3732 Fermentum Road","20":"P.O. Box 780, 2133 Rutrum Road","21":"Ap #400-1963 Pellentesque. Ave","22":"839 Cras Road","23":"361-5600 Per St.","24":"589-6397 Scelerisque Road","25":"Ap #142-8392 Consequat Avenue","26":"Ap #878-314 Faucibus Rd.","27":"1659 Risus. Street","28":"P.O. Box 143, 5197 At Avenue","29":"356-7423 Semper Road"},"City":{"0":"Lamontz\u017de","1":"Springfield","2":"Herne","3":"Elversele","4":"Osimo","5":"Lac-Serent","6":"Lehrte","7":"Arendonk","8":"Mataram","9":"Casciana Terme","10":"Manfredonia","11":"Valuyki","12":"Guaymas","13":"Valcourt","14":"Palermo","15":"Arquata del Tronto","16":"Chesapeake","17":"Maastricht","18":"Reading","19":"Sindelfingen","20":"Bromyard","21":"Aparecida de Goi\u00e2nia","22":"Etawah","23":"Mission","24":"Pozzuolo del Friuli","25":"Khammam","26":"Requ\u00ednoa","27":"Valuyki","28":"San Costantino Calabro","29":"Cardiff"},"ZipCode":{"0":"12144-86218","1":"6373","2":"?","3":"93452","4":"20619","5":"4464","6":"21449","7":"00636","8":"43168","9":"78450","10":"09534","11":"903927","12":"21574","13":"6387","14":"233311","15":"15936","16":"W2A 0QO","17":"754025","18":"27665","19":"678968","20":"Z7366","21":"744476","22":"15197","23":"0787 KM","24":"T5G 6M8","25":"49399","26":"1640","27":"12758","28":"77234","29":"Z4197"},"Date":{"0":"02.24.21","1":"10.15.20","2":"06.28.21","3":"?","4":"09.23.20","5":"12.22.21","6":"09.12.21","7":"02.15.22","8":"06.20.21","9":"02.12.22","10":"12.14.20","11":"10.31.21","12":"10.20.20","13":"12.01.21","14":"12.27.20","15":"02.06.21","16":"12.01.21","17":"01.06.21","18":"08.03.22","19":"11.20.20","20":"07.02.21","21":"12.21.21","22":"11.29.20","23":"08.08.22","24":"05.12.21","25":"02.06.21","26":"06.10.22","27":"02.03.21","28":"10.31.21","29":"05.28.22"},"Score":{"0":21.0,"1":21.0,"2":23.0,"3":null,"4":24.0,"5":17.0,"6":22.0,"7":21.0,"8":15.0,"9":19.0,"10":23.0,"11":14.0,"12":19.0,"13":25.0,"14":17.0,"15":21.0,"16":19.0,"17":13.0,"18":24.0,"19":11.0,"20":14.0,"21":17.0,"22":24.0,"23":19.0,"24":23.0,"25":20.0,"26":10.0,"27":20.0,"28":18.0,"29":19.0}}
Overwriting /tmp/data.json
[8]:
!cat /tmp/data.json
{"Name":{"0":"Isabelle I. Dotson","1":"Erin F. Munoz","2":"Troy G. Harrell","3":"Yen W. Summers","4":"Jerome B. Carney","5":"Yuri O. Head","6":"Ima A. Richard","7":"Eleanor Q. Guerrero","8":"Stuart F. Daniels","9":"Cara G. Beach","10":"Nolan E. Cortez","11":"Darius H. Beach","12":"Christian T. Mercer","13":"Florence F. Gordon","14":"Wilma I. Patton","15":"Rajah I. Jensen","16":"Zoe U. Whitfield","17":"May K. Wood","18":"Beck Q. Monroe","19":"Amanda A. Marks","20":"Serina X. Lang","21":"Lionel L. Bartlett","22":"Florence Z. Oconnor","23":"Chloe Y. Sears","24":"Mariam N. Tyler","25":"Dana A. Jefferson","26":"Nola N. Chan","27":"Owen Z. Odom","28":"Serina B. Nash","29":"Salvador I. Powell"},"Address":{"0":"?","1":"P.O. Box 737, 6015 Ligula St.","2":"3706 Ut St.","3":"P.O. Box 939, 8016 Egestas Av.","4":"298-7004 Natoque St.","5":"P.O. Box 214, 231 Dapibus Ave","6":"Ap #496-7181 Ullamcorper, St.","7":"5425 Ornare St.","8":"719-8478 Nunc St.","9":"648-517 Velit. Av.","10":"Ap #766-6998 Gravida Rd.","11":"Ap #251-7151 Donec Av.","12":"P.O. Box 858, 1997 Elit, Avenue","13":"543-831 Est. Rd.","14":"P.O. Box 299, 7144 Orci Street","15":"Ap #782-6146 Turpis St.","16":"Ap #200-7243 Sit St.","17":"777-1067 Auctor, Ave","18":"P.O. Box 123, 9085 Lorem, Rd.","19":"638-3732 Fermentum Road","20":"P.O. Box 780, 2133 Rutrum Road","21":"Ap #400-1963 Pellentesque. Ave","22":"839 Cras Road","23":"361-5600 Per St.","24":"589-6397 Scelerisque Road","25":"Ap #142-8392 Consequat Avenue","26":"Ap #878-314 Faucibus Rd.","27":"1659 Risus. Street","28":"P.O. Box 143, 5197 At Avenue","29":"356-7423 Semper Road"},"City":{"0":"Lamontz\u017de","1":"Springfield","2":"Herne","3":"Elversele","4":"Osimo","5":"Lac-Serent","6":"Lehrte","7":"Arendonk","8":"Mataram","9":"Casciana Terme","10":"Manfredonia","11":"Valuyki","12":"Guaymas","13":"Valcourt","14":"Palermo","15":"Arquata del Tronto","16":"Chesapeake","17":"Maastricht","18":"Reading","19":"Sindelfingen","20":"Bromyard","21":"Aparecida de Goi\u00e2nia","22":"Etawah","23":"Mission","24":"Pozzuolo del Friuli","25":"Khammam","26":"Requ\u00ednoa","27":"Valuyki","28":"San Costantino Calabro","29":"Cardiff"},"ZipCode":{"0":"12144-86218","1":"6373","2":"?","3":"93452","4":"20619","5":"4464","6":"21449","7":"00636","8":"43168","9":"78450","10":"09534","11":"903927","12":"21574","13":"6387","14":"233311","15":"15936","16":"W2A 0QO","17":"754025","18":"27665","19":"678968","20":"Z7366","21":"744476","22":"15197","23":"0787 KM","24":"T5G 6M8","25":"49399","26":"1640","27":"12758","28":"77234","29":"Z4197"},"Date":{"0":"02.24.21","1":"10.15.20","2":"06.28.21","3":"?","4":"09.23.20","5":"12.22.21","6":"09.12.21","7":"02.15.22","8":"06.20.21","9":"02.12.22","10":"12.14.20","11":"10.31.21","12":"10.20.20","13":"12.01.21","14":"12.27.20","15":"02.06.21","16":"12.01.21","17":"01.06.21","18":"08.03.22","19":"11.20.20","20":"07.02.21","21":"12.21.21","22":"11.29.20","23":"08.08.22","24":"05.12.21","25":"02.06.21","26":"06.10.22","27":"02.03.21","28":"10.31.21","29":"05.28.22"},"Score":{"0":21.0,"1":21.0,"2":23.0,"3":null,"4":24.0,"5":17.0,"6":22.0,"7":21.0,"8":15.0,"9":19.0,"10":23.0,"11":14.0,"12":19.0,"13":25.0,"14":17.0,"15":21.0,"16":19.0,"17":13.0,"18":24.0,"19":11.0,"20":14.0,"21":17.0,"22":24.0,"23":19.0,"24":23.0,"25":20.0,"26":10.0,"27":20.0,"28":18.0,"29":19.0}}
[9]:
#
# Importación
# ===============================================
#
with open("/tmp/data.json", 'r') as json_file:
data_json = json.load(json_file)
type(data_json)
[9]:
dict
[10]:
#
# Claves del diccionario
#
data_json.keys()
[10]:
dict_keys(['Name', 'Address', 'City', 'ZipCode', 'Date', 'Score'])
[11]:
#
# Extracción de una columna
#
data_json['Name']
[11]:
{'0': 'Isabelle I. Dotson',
'1': 'Erin F. Munoz',
'2': 'Troy G. Harrell',
'3': 'Yen W. Summers',
'4': 'Jerome B. Carney',
'5': 'Yuri O. Head',
'6': 'Ima A. Richard',
'7': 'Eleanor Q. Guerrero',
'8': 'Stuart F. Daniels',
'9': 'Cara G. Beach',
'10': 'Nolan E. Cortez',
'11': 'Darius H. Beach',
'12': 'Christian T. Mercer',
'13': 'Florence F. Gordon',
'14': 'Wilma I. Patton',
'15': 'Rajah I. Jensen',
'16': 'Zoe U. Whitfield',
'17': 'May K. Wood',
'18': 'Beck Q. Monroe',
'19': 'Amanda A. Marks',
'20': 'Serina X. Lang',
'21': 'Lionel L. Bartlett',
'22': 'Florence Z. Oconnor',
'23': 'Chloe Y. Sears',
'24': 'Mariam N. Tyler',
'25': 'Dana A. Jefferson',
'26': 'Nola N. Chan',
'27': 'Owen Z. Odom',
'28': 'Serina B. Nash',
'29': 'Salvador I. Powell'}
[12]:
#
# Conversión a un string
# ===============================================
#
json_string = json.dumps(
data_json,
sort_keys=True,
indent=4,
)
json_string
[12]:
'{\n "Address": {\n "0": "?",\n "1": "P.O. Box 737, 6015 Ligula St.",\n "10": "Ap #766-6998 Gravida Rd.",\n "11": "Ap #251-7151 Donec Av.",\n "12": "P.O. Box 858, 1997 Elit, Avenue",\n "13": "543-831 Est. Rd.",\n "14": "P.O. Box 299, 7144 Orci Street",\n "15": "Ap #782-6146 Turpis St.",\n "16": "Ap #200-7243 Sit St.",\n "17": "777-1067 Auctor, Ave",\n "18": "P.O. Box 123, 9085 Lorem, Rd.",\n "19": "638-3732 Fermentum Road",\n "2": "3706 Ut St.",\n "20": "P.O. Box 780, 2133 Rutrum Road",\n "21": "Ap #400-1963 Pellentesque. Ave",\n "22": "839 Cras Road",\n "23": "361-5600 Per St.",\n "24": "589-6397 Scelerisque Road",\n "25": "Ap #142-8392 Consequat Avenue",\n "26": "Ap #878-314 Faucibus Rd.",\n "27": "1659 Risus. Street",\n "28": "P.O. Box 143, 5197 At Avenue",\n "29": "356-7423 Semper Road",\n "3": "P.O. Box 939, 8016 Egestas Av.",\n "4": "298-7004 Natoque St.",\n "5": "P.O. Box 214, 231 Dapibus Ave",\n "6": "Ap #496-7181 Ullamcorper, St.",\n "7": "5425 Ornare St.",\n "8": "719-8478 Nunc St.",\n "9": "648-517 Velit. Av."\n },\n "City": {\n "0": "Lamontz\\u017de",\n "1": "Springfield",\n "10": "Manfredonia",\n "11": "Valuyki",\n "12": "Guaymas",\n "13": "Valcourt",\n "14": "Palermo",\n "15": "Arquata del Tronto",\n "16": "Chesapeake",\n "17": "Maastricht",\n "18": "Reading",\n "19": "Sindelfingen",\n "2": "Herne",\n "20": "Bromyard",\n "21": "Aparecida de Goi\\u00e2nia",\n "22": "Etawah",\n "23": "Mission",\n "24": "Pozzuolo del Friuli",\n "25": "Khammam",\n "26": "Requ\\u00ednoa",\n "27": "Valuyki",\n "28": "San Costantino Calabro",\n "29": "Cardiff",\n "3": "Elversele",\n "4": "Osimo",\n "5": "Lac-Serent",\n "6": "Lehrte",\n "7": "Arendonk",\n "8": "Mataram",\n "9": "Casciana Terme"\n },\n "Date": {\n "0": "02.24.21",\n "1": "10.15.20",\n "10": "12.14.20",\n "11": "10.31.21",\n "12": "10.20.20",\n "13": "12.01.21",\n "14": "12.27.20",\n "15": "02.06.21",\n "16": "12.01.21",\n "17": "01.06.21",\n "18": "08.03.22",\n "19": "11.20.20",\n "2": "06.28.21",\n "20": "07.02.21",\n "21": "12.21.21",\n "22": "11.29.20",\n "23": "08.08.22",\n "24": "05.12.21",\n "25": "02.06.21",\n "26": "06.10.22",\n "27": "02.03.21",\n "28": "10.31.21",\n "29": "05.28.22",\n "3": "?",\n "4": "09.23.20",\n "5": "12.22.21",\n "6": "09.12.21",\n "7": "02.15.22",\n "8": "06.20.21",\n "9": "02.12.22"\n },\n "Name": {\n "0": "Isabelle I. Dotson",\n "1": "Erin F. Munoz",\n "10": "Nolan E. Cortez",\n "11": "Darius H. Beach",\n "12": "Christian T. Mercer",\n "13": "Florence F. Gordon",\n "14": "Wilma I. Patton",\n "15": "Rajah I. Jensen",\n "16": "Zoe U. Whitfield",\n "17": "May K. Wood",\n "18": "Beck Q. Monroe",\n "19": "Amanda A. Marks",\n "2": "Troy G. Harrell",\n "20": "Serina X. Lang",\n "21": "Lionel L. Bartlett",\n "22": "Florence Z. Oconnor",\n "23": "Chloe Y. Sears",\n "24": "Mariam N. Tyler",\n "25": "Dana A. Jefferson",\n "26": "Nola N. Chan",\n "27": "Owen Z. Odom",\n "28": "Serina B. Nash",\n "29": "Salvador I. Powell",\n "3": "Yen W. Summers",\n "4": "Jerome B. Carney",\n "5": "Yuri O. Head",\n "6": "Ima A. Richard",\n "7": "Eleanor Q. Guerrero",\n "8": "Stuart F. Daniels",\n "9": "Cara G. Beach"\n },\n "Score": {\n "0": 21.0,\n "1": 21.0,\n "10": 23.0,\n "11": 14.0,\n "12": 19.0,\n "13": 25.0,\n "14": 17.0,\n "15": 21.0,\n "16": 19.0,\n "17": 13.0,\n "18": 24.0,\n "19": 11.0,\n "2": 23.0,\n "20": 14.0,\n "21": 17.0,\n "22": 24.0,\n "23": 19.0,\n "24": 23.0,\n "25": 20.0,\n "26": 10.0,\n "27": 20.0,\n "28": 18.0,\n "29": 19.0,\n "3": null,\n "4": 24.0,\n "5": 17.0,\n "6": 22.0,\n "7": 21.0,\n "8": 15.0,\n "9": 19.0\n },\n "ZipCode": {\n "0": "12144-86218",\n "1": "6373",\n "10": "09534",\n "11": "903927",\n "12": "21574",\n "13": "6387",\n "14": "233311",\n "15": "15936",\n "16": "W2A 0QO",\n "17": "754025",\n "18": "27665",\n "19": "678968",\n "2": "?",\n "20": "Z7366",\n "21": "744476",\n "22": "15197",\n "23": "0787 KM",\n "24": "T5G 6M8",\n "25": "49399",\n "26": "1640",\n "27": "12758",\n "28": "77234",\n "29": "Z4197",\n "3": "93452",\n "4": "20619",\n "5": "4464",\n "6": "21449",\n "7": "00636",\n "8": "43168",\n "9": "78450"\n }\n}'
[13]:
#
# Impresion
# ===============================================
#
print(json_string)
{
"Address": {
"0": "?",
"1": "P.O. Box 737, 6015 Ligula St.",
"10": "Ap #766-6998 Gravida Rd.",
"11": "Ap #251-7151 Donec Av.",
"12": "P.O. Box 858, 1997 Elit, Avenue",
"13": "543-831 Est. Rd.",
"14": "P.O. Box 299, 7144 Orci Street",
"15": "Ap #782-6146 Turpis St.",
"16": "Ap #200-7243 Sit St.",
"17": "777-1067 Auctor, Ave",
"18": "P.O. Box 123, 9085 Lorem, Rd.",
"19": "638-3732 Fermentum Road",
"2": "3706 Ut St.",
"20": "P.O. Box 780, 2133 Rutrum Road",
"21": "Ap #400-1963 Pellentesque. Ave",
"22": "839 Cras Road",
"23": "361-5600 Per St.",
"24": "589-6397 Scelerisque Road",
"25": "Ap #142-8392 Consequat Avenue",
"26": "Ap #878-314 Faucibus Rd.",
"27": "1659 Risus. Street",
"28": "P.O. Box 143, 5197 At Avenue",
"29": "356-7423 Semper Road",
"3": "P.O. Box 939, 8016 Egestas Av.",
"4": "298-7004 Natoque St.",
"5": "P.O. Box 214, 231 Dapibus Ave",
"6": "Ap #496-7181 Ullamcorper, St.",
"7": "5425 Ornare St.",
"8": "719-8478 Nunc St.",
"9": "648-517 Velit. Av."
},
"City": {
"0": "Lamontz\u017de",
"1": "Springfield",
"10": "Manfredonia",
"11": "Valuyki",
"12": "Guaymas",
"13": "Valcourt",
"14": "Palermo",
"15": "Arquata del Tronto",
"16": "Chesapeake",
"17": "Maastricht",
"18": "Reading",
"19": "Sindelfingen",
"2": "Herne",
"20": "Bromyard",
"21": "Aparecida de Goi\u00e2nia",
"22": "Etawah",
"23": "Mission",
"24": "Pozzuolo del Friuli",
"25": "Khammam",
"26": "Requ\u00ednoa",
"27": "Valuyki",
"28": "San Costantino Calabro",
"29": "Cardiff",
"3": "Elversele",
"4": "Osimo",
"5": "Lac-Serent",
"6": "Lehrte",
"7": "Arendonk",
"8": "Mataram",
"9": "Casciana Terme"
},
"Date": {
"0": "02.24.21",
"1": "10.15.20",
"10": "12.14.20",
"11": "10.31.21",
"12": "10.20.20",
"13": "12.01.21",
"14": "12.27.20",
"15": "02.06.21",
"16": "12.01.21",
"17": "01.06.21",
"18": "08.03.22",
"19": "11.20.20",
"2": "06.28.21",
"20": "07.02.21",
"21": "12.21.21",
"22": "11.29.20",
"23": "08.08.22",
"24": "05.12.21",
"25": "02.06.21",
"26": "06.10.22",
"27": "02.03.21",
"28": "10.31.21",
"29": "05.28.22",
"3": "?",
"4": "09.23.20",
"5": "12.22.21",
"6": "09.12.21",
"7": "02.15.22",
"8": "06.20.21",
"9": "02.12.22"
},
"Name": {
"0": "Isabelle I. Dotson",
"1": "Erin F. Munoz",
"10": "Nolan E. Cortez",
"11": "Darius H. Beach",
"12": "Christian T. Mercer",
"13": "Florence F. Gordon",
"14": "Wilma I. Patton",
"15": "Rajah I. Jensen",
"16": "Zoe U. Whitfield",
"17": "May K. Wood",
"18": "Beck Q. Monroe",
"19": "Amanda A. Marks",
"2": "Troy G. Harrell",
"20": "Serina X. Lang",
"21": "Lionel L. Bartlett",
"22": "Florence Z. Oconnor",
"23": "Chloe Y. Sears",
"24": "Mariam N. Tyler",
"25": "Dana A. Jefferson",
"26": "Nola N. Chan",
"27": "Owen Z. Odom",
"28": "Serina B. Nash",
"29": "Salvador I. Powell",
"3": "Yen W. Summers",
"4": "Jerome B. Carney",
"5": "Yuri O. Head",
"6": "Ima A. Richard",
"7": "Eleanor Q. Guerrero",
"8": "Stuart F. Daniels",
"9": "Cara G. Beach"
},
"Score": {
"0": 21.0,
"1": 21.0,
"10": 23.0,
"11": 14.0,
"12": 19.0,
"13": 25.0,
"14": 17.0,
"15": 21.0,
"16": 19.0,
"17": 13.0,
"18": 24.0,
"19": 11.0,
"2": 23.0,
"20": 14.0,
"21": 17.0,
"22": 24.0,
"23": 19.0,
"24": 23.0,
"25": 20.0,
"26": 10.0,
"27": 20.0,
"28": 18.0,
"29": 19.0,
"3": null,
"4": 24.0,
"5": 17.0,
"6": 22.0,
"7": 21.0,
"8": 15.0,
"9": 19.0
},
"ZipCode": {
"0": "12144-86218",
"1": "6373",
"10": "09534",
"11": "903927",
"12": "21574",
"13": "6387",
"14": "233311",
"15": "15936",
"16": "W2A 0QO",
"17": "754025",
"18": "27665",
"19": "678968",
"2": "?",
"20": "Z7366",
"21": "744476",
"22": "15197",
"23": "0787 KM",
"24": "T5G 6M8",
"25": "49399",
"26": "1640",
"27": "12758",
"28": "77234",
"29": "Z4197",
"3": "93452",
"4": "20619",
"5": "4464",
"6": "21449",
"7": "00636",
"8": "43168",
"9": "78450"
}
}
[14]:
#
# Importación desde un string
# ===============================================
#
json_string = """
{
"Name": {
"0": "Jameson R. Boyle",
"1": "Idona J. Singleton",
"2": "Ryder K. Maldonado"
},
"Phone": {
"0": "1-507-132-4340",
"1": "1-662-859-5562",
"2": "1-921-715-5315"
}
}
"""
json.loads(json_string)
[14]:
{'Name': {'0': 'Jameson R. Boyle',
'1': 'Idona J. Singleton',
'2': 'Ryder K. Maldonado'},
'Phone': {'0': '1-507-132-4340',
'1': '1-662-859-5562',
'2': '1-921-715-5315'}}
Ingestión desde una API
[15]:
import requests
from pprint import pprint
username = "jdvelasq"
url = f"https://api.github.com/users/{username}"
user_data = requests.get(url).json()
pprint(user_data)
{'avatar_url': 'https://avatars.githubusercontent.com/u/8204100?v=4',
'bio': 'Full Professor (Analytics and Computer Science) at Facultad de Minas, '
'Universidad Nacional de Colombia. ',
'blog': '',
'company': 'Universidad Nacional de Colombia',
'created_at': '2014-07-18T19:26:25Z',
'email': None,
'events_url': 'https://api.github.com/users/jdvelasq/events{/privacy}',
'followers': 369,
'followers_url': 'https://api.github.com/users/jdvelasq/followers',
'following': 0,
'following_url': 'https://api.github.com/users/jdvelasq/following{/other_user}',
'gists_url': 'https://api.github.com/users/jdvelasq/gists{/gist_id}',
'gravatar_id': '',
'hireable': None,
'html_url': 'https://github.com/jdvelasq',
'id': 8204100,
'location': 'Medellín, Colombia',
'login': 'jdvelasq',
'name': 'Juan David Velásquez-Henao',
'node_id': 'MDQ6VXNlcjgyMDQxMDA=',
'organizations_url': 'https://api.github.com/users/jdvelasq/orgs',
'public_gists': 3,
'public_repos': 54,
'received_events_url': 'https://api.github.com/users/jdvelasq/received_events',
'repos_url': 'https://api.github.com/users/jdvelasq/repos',
'site_admin': False,
'starred_url': 'https://api.github.com/users/jdvelasq/starred{/owner}{/repo}',
'subscriptions_url': 'https://api.github.com/users/jdvelasq/subscriptions',
'twitter_username': None,
'type': 'User',
'updated_at': '2021-08-10T14:33:27Z',
'url': 'https://api.github.com/users/jdvelasq'}
[16]:
#
# Extracción de un valor
#
user_data['name']
[16]:
'Juan David Velásquez-Henao'
[17]:
#
# Extracción de los repositorios
#
repos_url = user_data['repos_url']
repos_data = requests.get(repos_url).json()
pprint(repos_data[2])
{'archive_url': 'https://api.github.com/repos/jdvelasq/autograding-run-command/{archive_format}{/ref}',
'archived': False,
'assignees_url': 'https://api.github.com/repos/jdvelasq/autograding-run-command/assignees{/user}',
'blobs_url': 'https://api.github.com/repos/jdvelasq/autograding-run-command/git/blobs{/sha}',
'branches_url': 'https://api.github.com/repos/jdvelasq/autograding-run-command/branches{/branch}',
'clone_url': 'https://github.com/jdvelasq/autograding-run-command.git',
'collaborators_url': 'https://api.github.com/repos/jdvelasq/autograding-run-command/collaborators{/collaborator}',
'comments_url': 'https://api.github.com/repos/jdvelasq/autograding-run-command/comments{/number}',
'commits_url': 'https://api.github.com/repos/jdvelasq/autograding-run-command/commits{/sha}',
'compare_url': 'https://api.github.com/repos/jdvelasq/autograding-run-command/compare/{base}...{head}',
'contents_url': 'https://api.github.com/repos/jdvelasq/autograding-run-command/contents/{+path}',
'contributors_url': 'https://api.github.com/repos/jdvelasq/autograding-run-command/contributors',
'created_at': '2021-02-14T01:24:40Z',
'default_branch': 'main',
'deployments_url': 'https://api.github.com/repos/jdvelasq/autograding-run-command/deployments',
'description': None,
'disabled': False,
'downloads_url': 'https://api.github.com/repos/jdvelasq/autograding-run-command/downloads',
'events_url': 'https://api.github.com/repos/jdvelasq/autograding-run-command/events',
'fork': False,
'forks': 0,
'forks_count': 0,
'forks_url': 'https://api.github.com/repos/jdvelasq/autograding-run-command/forks',
'full_name': 'jdvelasq/autograding-run-command',
'git_commits_url': 'https://api.github.com/repos/jdvelasq/autograding-run-command/git/commits{/sha}',
'git_refs_url': 'https://api.github.com/repos/jdvelasq/autograding-run-command/git/refs{/sha}',
'git_tags_url': 'https://api.github.com/repos/jdvelasq/autograding-run-command/git/tags{/sha}',
'git_url': 'git://github.com/jdvelasq/autograding-run-command.git',
'has_downloads': True,
'has_issues': True,
'has_pages': False,
'has_projects': True,
'has_wiki': True,
'homepage': None,
'hooks_url': 'https://api.github.com/repos/jdvelasq/autograding-run-command/hooks',
'html_url': 'https://github.com/jdvelasq/autograding-run-command',
'id': 338706086,
'issue_comment_url': 'https://api.github.com/repos/jdvelasq/autograding-run-command/issues/comments{/number}',
'issue_events_url': 'https://api.github.com/repos/jdvelasq/autograding-run-command/issues/events{/number}',
'issues_url': 'https://api.github.com/repos/jdvelasq/autograding-run-command/issues{/number}',
'keys_url': 'https://api.github.com/repos/jdvelasq/autograding-run-command/keys{/key_id}',
'labels_url': 'https://api.github.com/repos/jdvelasq/autograding-run-command/labels{/name}',
'language': 'Shell',
'languages_url': 'https://api.github.com/repos/jdvelasq/autograding-run-command/languages',
'license': None,
'merges_url': 'https://api.github.com/repos/jdvelasq/autograding-run-command/merges',
'milestones_url': 'https://api.github.com/repos/jdvelasq/autograding-run-command/milestones{/number}',
'mirror_url': None,
'name': 'autograding-run-command',
'node_id': 'MDEwOlJlcG9zaXRvcnkzMzg3MDYwODY=',
'notifications_url': 'https://api.github.com/repos/jdvelasq/autograding-run-command/notifications{?since,all,participating}',
'open_issues': 0,
'open_issues_count': 0,
'owner': {'avatar_url': 'https://avatars.githubusercontent.com/u/8204100?v=4',
'events_url': 'https://api.github.com/users/jdvelasq/events{/privacy}',
'followers_url': 'https://api.github.com/users/jdvelasq/followers',
'following_url': 'https://api.github.com/users/jdvelasq/following{/other_user}',
'gists_url': 'https://api.github.com/users/jdvelasq/gists{/gist_id}',
'gravatar_id': '',
'html_url': 'https://github.com/jdvelasq',
'id': 8204100,
'login': 'jdvelasq',
'node_id': 'MDQ6VXNlcjgyMDQxMDA=',
'organizations_url': 'https://api.github.com/users/jdvelasq/orgs',
'received_events_url': 'https://api.github.com/users/jdvelasq/received_events',
'repos_url': 'https://api.github.com/users/jdvelasq/repos',
'site_admin': False,
'starred_url': 'https://api.github.com/users/jdvelasq/starred{/owner}{/repo}',
'subscriptions_url': 'https://api.github.com/users/jdvelasq/subscriptions',
'type': 'User',
'url': 'https://api.github.com/users/jdvelasq'},
'private': False,
'pulls_url': 'https://api.github.com/repos/jdvelasq/autograding-run-command/pulls{/number}',
'pushed_at': '2021-02-14T01:29:01Z',
'releases_url': 'https://api.github.com/repos/jdvelasq/autograding-run-command/releases{/id}',
'size': 3,
'ssh_url': 'git@github.com:jdvelasq/autograding-run-command.git',
'stargazers_count': 0,
'stargazers_url': 'https://api.github.com/repos/jdvelasq/autograding-run-command/stargazers',
'statuses_url': 'https://api.github.com/repos/jdvelasq/autograding-run-command/statuses/{sha}',
'subscribers_url': 'https://api.github.com/repos/jdvelasq/autograding-run-command/subscribers',
'subscription_url': 'https://api.github.com/repos/jdvelasq/autograding-run-command/subscription',
'svn_url': 'https://github.com/jdvelasq/autograding-run-command',
'tags_url': 'https://api.github.com/repos/jdvelasq/autograding-run-command/tags',
'teams_url': 'https://api.github.com/repos/jdvelasq/autograding-run-command/teams',
'trees_url': 'https://api.github.com/repos/jdvelasq/autograding-run-command/git/trees{/sha}',
'updated_at': '2021-02-14T01:30:19Z',
'url': 'https://api.github.com/repos/jdvelasq/autograding-run-command',
'watchers': 0,
'watchers_count': 0}