Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeRoor reading json
#1
Hi,
I’m trying to read a json url however I’ve got this error:
“TypeError: list indices must be integers or slices, not str”

This is de json url: https://servicios.ine.es/wstempus/js/es/...tip=AM&
and the idea is see it in jupyter notebook as a table with this columns
“Año”, “Municipio”, “De 0 a 4 años”, “De 5 a 9 años”, “De 10 a 14 años”, etc, but I can’t do it
Any idea or help?

Thank you
Reply
#2
Show your code and full traceback you get. It looks you get and parse the JSON response and then get error when try to use it.

Also show expected output because the JSON is nested
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Hi buran and thank you,

This is my code

response = requests.get(path_json_crecimientopob)
data = response.json()['data']
data

and have this error

"---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_1880\2903580319.py in <module>
1 response = requests.get(path_json_crecimientopob)
----> 2 data = response.json()['data']
3 data

TypeError: list indices must be integers or slices, not str"

I use this code for other json and then it was transformed into dataframe to work with them, but in this case don't work, I new in python and probably I can't see the solution.

The result should be a table with many columns:

'Municipio'
'Año'
'De 0 a 4 años'
'De 5 a 9 años'
'De 10 a 14 años'
'De 15 a 64 años'
'De 15 a 19 años'
'De 20 a 24 años'
'De 25 a 29 años'
'De 30 a 34 años'
'De 35 a 39 años'
'De 40 a 44 años'
'De 45 a 49 años'
'De 50 a 54 años'
'De 55 a 59 años'
'De 60 a 64 años'
'De 65 a 69 años'
'De 70 a 74 años'
'De 75 a 79 años'
'De 80 a 84 años'
'De 85 a 89 años'
'De 90 a 94 años'
'De 95 a 99 años'
'De 100 y más años'
Reply
#4
Your json is array of 6864 jsonobjects, that is deserialised to list of dicts, like this (one elment):
Output:
{ "COD": "PC97987", "Nombre": "Todas las edades. Total. Total. Málaga. Dato base. ", "T3_Unidad": "Personas", "T3_Escala": " ", "MetaData": [{ "Id": 15668, "Variable": { "Id": 356, "Nombre": "Totales de edad", "Codigo": "" }, "Nombre": "Todas las edades", "Codigo": "" }, { "Id": 16420, "Variable": { "Id": 141, "Nombre": "Nacionalidad", "Codigo": "" }, "Nombre": "Total", "Codigo": "" }, { "Id": 451, "Variable": { "Id": 18, "Nombre": "Sexo", "Codigo": "" }, "Nombre": "Total", "Codigo": "" }, { "Id": 30, "Variable": { "Id": 115, "Nombre": "Provincias", "Codigo": "PROV" }, "Nombre": "Málaga", "Codigo": "29" }, { "Id": 72, "Variable": { "Id": 3, "Nombre": "Tipo de dato", "Codigo": "" }, "Nombre": "Dato base", "Codigo": "" }], "Data": [{ "Fecha": "2022-01-01T00:00:00.000+01:00", "T3_TipoDato": "Definitivo", "T3_Periodo": "1 de enero de", "Anyo": 2022, "Valor": 1717504.0 }, { "Fecha": "2021-01-01T00:00:00.000+01:00", "T3_TipoDato": "Definitivo", "T3_Periodo": "1 de enero de", "Anyo": 2021, "Valor": 1695651.0 }, { "Fecha": "2020-01-01T00:00:00.000+01:00", "T3_TipoDato": "Definitivo", "T3_Periodo": "1 de enero de", "Anyo": 2020, "Valor": 1685920.0 }, { "Fecha": "2019-01-01T00:00:00.000+01:00", "T3_TipoDato": "Definitivo", "T3_Periodo": "1 de enero de", "Anyo": 2019, "Valor": 1661785.0 }, { "Fecha": "2018-01-01T00:00:00.000+01:00", "T3_TipoDato": "Definitivo", "T3_Periodo": "1 de enero de", "Anyo": 2018, "Valor": 1641121.0 }, { "Fecha": "2017-01-01T00:00:00.000+01:00", "T3_TipoDato": "Definitivo", "T3_Periodo": "1 de enero de", "Anyo": 2017, "Valor": 1630615.0 }, { "Fecha": "2016-01-01T00:00:00.000+01:00", "T3_TipoDato": "Definitivo", "T3_Periodo": "1 de enero de", "Anyo": 2016, "Valor": 1629298.0 }, { "Fecha": "2015-01-01T00:00:00.000+01:00", "T3_TipoDato": "Definitivo", "T3_Periodo": "1 de enero de", "Anyo": 2015, "Valor": 1628973.0 }, { "Fecha": "2014-01-01T00:00:00.000+01:00", "T3_TipoDato": "Definitivo", "T3_Periodo": "1 de enero de", "Anyo": 2014, "Valor": 1621968.0 }, { "Fecha": "2013-01-01T00:00:00.000+01:00", "T3_TipoDato": "Definitivo", "T3_Periodo": "1 de enero de", "Anyo": 2013, "Valor": 1652999.0 }, { "Fecha": "2012-01-01T00:00:00.000+01:00", "T3_TipoDato": "Definitivo", "T3_Periodo": "1 de enero de", "Anyo": 2012, "Valor": 1641098.0 }, { "Fecha": "2011-01-01T00:00:00.000+01:00", "T3_TipoDato": "Definitivo", "T3_Periodo": "1 de enero de", "Anyo": 2011, "Valor": 1625827.0 }, { "Fecha": "2010-01-01T00:00:00.000+01:00", "T3_TipoDato": "Definitivo", "T3_Periodo": "1 de enero de", "Anyo": 2010, "Valor": 1609557.0 }, { "Fecha": "2009-01-01T00:00:00.000+01:00", "T3_TipoDato": "Definitivo", "T3_Periodo": "1 de enero de", "Anyo": 2009, "Valor": 1593068.0 }, { "Fecha": "2008-01-01T00:00:00.000+01:00", "T3_TipoDato": "Definitivo", "T3_Periodo": "1 de enero de", "Anyo": 2008, "Valor": 1563261.0 }, { "Fecha": "2007-01-01T00:00:00.000+01:00", "T3_TipoDato": "Definitivo", "T3_Periodo": "1 de enero de", "Anyo": 2007, "Valor": 1517523.0 }, { "Fecha": "2006-01-01T00:00:00.000+01:00", "T3_TipoDato": "Definitivo", "T3_Periodo": "1 de enero de", "Anyo": 2006, "Valor": 1491287.0 }, { "Fecha": "2005-01-01T00:00:00.000+01:00", "T3_TipoDato": "Definitivo", "T3_Periodo": "1 de enero de", "Anyo": 2005, "Valor": 1453409.0 }, { "Fecha": "2004-01-01T00:00:00.000+01:00", "T3_TipoDato": "Definitivo", "T3_Periodo": "1 de enero de", "Anyo": 2004, "Valor": 1397925.0 }, { "Fecha": "2003-01-01T00:00:00.000+01:00", "T3_TipoDato": "Definitivo", "T3_Periodo": "1 de enero de", "Anyo": 2003, "Valor": 1374890.0 }] }
I don't see which part of this goes into which column
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading Data from JSON tpolim008 2 1,099 Sep-27-2022, 06:34 PM
Last Post: Larz60+
  Initializing, reading and updating a large JSON file medatib531 0 1,799 Mar-10-2022, 07:58 PM
Last Post: medatib531
  Help with reading json file hhchenfx 5 4,513 Jul-07-2021, 01:58 PM
Last Post: hhchenfx
  reading json file DrBrownStats 1 1,835 Nov-25-2020, 09:00 AM
Last Post: Larz60+
  Reading a json file ebolisa 2 2,174 Mar-15-2020, 09:24 AM
Last Post: ebolisa
  Reading json behind a login page ebolisa 3 2,443 May-26-2019, 05:15 PM
Last Post: heiner55
  Reading JSON - error jmair 2 2,248 May-22-2019, 07:25 PM
Last Post: jmair
  [split] Reading json from webpage rajesh1997 1 2,211 Feb-05-2019, 12:07 PM
Last Post: buran
  Reading json from webpage jmair 4 4,070 Jan-10-2019, 09:14 PM
Last Post: jmair
  Reading complex JSON dictionaries Gromis 5 3,855 Jul-08-2018, 10:26 PM
Last Post: Gromis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020