0

Hola necesito obtener los datos de este json, con JQuery, por ejemplo el dato que necesito es el de "confirmed":

Estoy tratando de consumir estos datos desde una api.

"{\"1\": {\"region\": \"Tarapacá\", \"regionData\": {\"04/13/2020\": {\"confirmed\": 52, \"confirmed_per_100k\": 15.73, \"deaths\": 0, \"deaths_per_100k\": 15.73}}, \"regionInfo\": {\"_id\": 1, \"lat\": -20.22036, \"long\": -70.13913, \"population\": 330558}}, \"10\": {\"region\": \"Los Lagos\", \"regionData\": {\"04/13/2020\": {\"confirmed\": 380, \"confirmed_per_100k\": 45.85, \"deaths\": 5, \"deaths_per_100k\": 45.85}}, \"regionInfo\": {\"_id\": 10, \"lat\": -42.300844, \"long\": -73.105387, \"population\": 828708}}, \"11\": {\"region\": \"Aysén\", \"regionData\": {\"04/13/2020\": {\"confirmed\": 7, \"confirmed_per_100k\": 6.79, \"deaths\": 0, \"deaths_per_100k\": 6.79}}, \"regionInfo\": {\"_id\": 11, \"lat\": -46.1434629, \"long\": -74.3648869, \"population\": 103158}}, \"12\": {\"region\": \"Magallanes\", \"regionData\": {\"04/13/2020\": {\"confirmed\": 415, \"confirmed_per_100k\": 249.2, \"deaths\": 5, \"deaths_per_100k\": 249.2}}, \"regionInfo\": {\"_id\": 12, \"lat\": -53.3527518, \"long\": -71.5547782, \"population\": 166533}}, \"13\": {\"region\": \"Metropolitana\", \"regionData\": {\"04/13/2020\": {\"confirmed\": 3803, \"confirmed_per_100k\": 53.47, \"deaths\": 36, \"deaths_per_100k\": 53.47}}, \"regionInfo\": {\"_id\": 13, \"lat\": -33.4372, \"long\": -70.6506, \"population\": 7112808}}, \"14\": {\"region\": \"Los Ríos\", \"regionData\": {\"04/13/2020\": {\"confirmed\": 138, \"confirmed_per_100k\": 14.42, \"deaths\": 3, \"deaths_per_100k\": 14.42}}, \"regionInfo\": {\"_id\": 14, \"lat\": -40.293129, \"long\": -73.0816727, \"population\": 957224}}, \"15\": {\"region\": \"Arica y Parinacota\", \"regionData\": {\"04/13/2020\": {\"confirmed\": 115, \"confirmed_per_100k\": 50.87, \"deaths\": 1, \"deaths_per_100k\": 50.87}}, \"regionInfo\": {\"_id\": 15, \"lat\": -18.478518, \"long\": -70.3210596, \"population\": 226068}}, \"16\": {\"region\": \"Ñuble\", \"regionData\": {\"04/13/2020\": {\"confirmed\": 618, \"confirmed_per_100k\": 128.59, \"deaths\": 7, \"deaths_per_100k\": 128.59}}, \"regionInfo\": {\"_id\": 16, \"lat\": -36.6331577, \"long\": -71.9384821, \"population\": 480609}}, \"2\": {\"region\": \"Antofagasta\", \"regionData\": {\"04/13/2020\": {\"confirmed\": 149, \"confirmed_per_100k\": 24.53, \"deaths\": 1, \"deaths_per_100k\": 24.53}}, \"regionInfo\": {\"_id\": 2, \"lat\": -23.6463741, \"long\": -70.3980033, \"population\": 607534}}, \"3\": {\"region\": \"Atacama\", \"regionData\": {\"04/13/2020\": {\"confirmed\": 13, \"confirmed_per_100k\": 4.54, \"deaths\": 0, \"deaths_per_100k\": 4.54}}, \"regionInfo\": {\"_id\": 3, \"lat\": -27.5571783, \"long\": -70.0156882, \"population\": 286168}}, \"4\": {\"region\": \"Coquimbo\", \"regionData\": {\"04/13/2020\": {\"confirmed\": 66, \"confirmed_per_100k\": 8.71, \"deaths\": 0, \"deaths_per_100k\": 8.71}}, \"regionInfo\": {\"_id\": 4, \"lat\": -30.7546652, \"long\": -70.9005536, \"population\": 757586}}, \"5\": {\"region\": \"Valparaíso\", \"regionData\": {\"04/13/2020\": {\"confirmed\": 273, \"confirmed_per_100k\": 15.03, \"deaths\": 2, \"deaths_per_100k\": 15.03}}, \"regionInfo\": {\"_id\": 5, \"lat\": -33.0458456, \"long\": -71.6196749, \"population\": 1815902}}, \"6\": {\"region\": \"O’Higgins\", \"regionData\": {\"04/13/2020\": {\"confirmed\": 48, \"confirmed_per_100k\": 5.25, \"deaths\": 0, \"deaths_per_100k\": 5.25}}, \"regionInfo\": {\"_id\": 6, \"lat\": -34.1708298, \"long\": -70.7444382, \"population\": 914555}}, \"7\": {\"region\": \"Maule\", \"regionData\": {\"04/13/2020\": {\"confirmed\": 141, \"confirmed_per_100k\": 13.49, \"deaths\": 3, \"deaths_per_100k\": 13.49}}, \"regionInfo\": {\"_id\": 7, \"lat\": -35.5972284, \"long\": -71.48868, \"population\": 1044950}}, \"8\": {\"region\": \"Biobío\", \"regionData\": {\"04/13/2020\": {\"confirmed\": 512, \"confirmed_per_100k\": 32.89, \"deaths\": 2, \"deaths_per_100k\": 32.89}}, \"regionInfo\": {\"_id\": 8, \"lat\": -37.3391407, \"long\": -72.4106825, \"population\": 1556805}}, \"9\": {\"region\": \"Araucanía\", \"regionData\": {\"04/13/2020\": {\"confirmed\": 795, \"confirmed_per_100k\": 83.05, \"deaths\": 17, \"deaths_per_100k\": 83.05}}, \"regionInfo\": {\"_id\": 9, \"lat\": -38.6710116, \"long\": -72.2564576, \"population\": 957224}}}"

GRACIAS

1
  • Ese es un string, creo que la pregunta necesita más detalles y lo que has intentado
    – Sr1871
    Commented el 28 may. 2020 a las 22:55

1 respuesta 1

0

Debes primero parsear el JSON que obtienes usando JSON.parse();

const json = "{\"1\": {\"region\": \"Tarapacá\", \"regionData\": {\"04/13/2020\": {\"confirmed\": 52, \"confirmed_per_100k\": 15.73, \"deaths\": 0, \"deaths_per_100k\": 15.73}}, \"regionInfo\": {\"_id\": 1, \"lat\": -20.22036, \"long\": -70.13913, \"population\": 330558}}, \"10\": {\"region\": \"Los Lagos\", \"regionData\": {\"04/13/2020\": {\"confirmed\": 380, \"confirmed_per_100k\": 45.85, \"deaths\": 5, \"deaths_per_100k\": 45.85}}, \"regionInfo\": {\"_id\": 10, \"lat\": -42.300844, \"long\": -73.105387, \"population\": 828708}}, \"11\": {\"region\": \"Aysén\", \"regionData\": {\"04/13/2020\": {\"confirmed\": 7, \"confirmed_per_100k\": 6.79, \"deaths\": 0, \"deaths_per_100k\": 6.79}}, \"regionInfo\": {\"_id\": 11, \"lat\": -46.1434629, \"long\": -74.3648869, \"population\": 103158}}, \"12\": {\"region\": \"Magallanes\", \"regionData\": {\"04/13/2020\": {\"confirmed\": 415, \"confirmed_per_100k\": 249.2, \"deaths\": 5, \"deaths_per_100k\": 249.2}}, \"regionInfo\": {\"_id\": 12, \"lat\": -53.3527518, \"long\": -71.5547782, \"population\": 166533}}, \"13\": {\"region\": \"Metropolitana\", \"regionData\": {\"04/13/2020\": {\"confirmed\": 3803, \"confirmed_per_100k\": 53.47, \"deaths\": 36, \"deaths_per_100k\": 53.47}}, \"regionInfo\": {\"_id\": 13, \"lat\": -33.4372, \"long\": -70.6506, \"population\": 7112808}}, \"14\": {\"region\": \"Los Ríos\", \"regionData\": {\"04/13/2020\": {\"confirmed\": 138, \"confirmed_per_100k\": 14.42, \"deaths\": 3, \"deaths_per_100k\": 14.42}}, \"regionInfo\": {\"_id\": 14, \"lat\": -40.293129, \"long\": -73.0816727, \"population\": 957224}}, \"15\": {\"region\": \"Arica y Parinacota\", \"regionData\": {\"04/13/2020\": {\"confirmed\": 115, \"confirmed_per_100k\": 50.87, \"deaths\": 1, \"deaths_per_100k\": 50.87}}, \"regionInfo\": {\"_id\": 15, \"lat\": -18.478518, \"long\": -70.3210596, \"population\": 226068}}, \"16\": {\"region\": \"Ñuble\", \"regionData\": {\"04/13/2020\": {\"confirmed\": 618, \"confirmed_per_100k\": 128.59, \"deaths\": 7, \"deaths_per_100k\": 128.59}}, \"regionInfo\": {\"_id\": 16, \"lat\": -36.6331577, \"long\": -71.9384821, \"population\": 480609}}, \"2\": {\"region\": \"Antofagasta\", \"regionData\": {\"04/13/2020\": {\"confirmed\": 149, \"confirmed_per_100k\": 24.53, \"deaths\": 1, \"deaths_per_100k\": 24.53}}, \"regionInfo\": {\"_id\": 2, \"lat\": -23.6463741, \"long\": -70.3980033, \"population\": 607534}}, \"3\": {\"region\": \"Atacama\", \"regionData\": {\"04/13/2020\": {\"confirmed\": 13, \"confirmed_per_100k\": 4.54, \"deaths\": 0, \"deaths_per_100k\": 4.54}}, \"regionInfo\": {\"_id\": 3, \"lat\": -27.5571783, \"long\": -70.0156882, \"population\": 286168}}, \"4\": {\"region\": \"Coquimbo\", \"regionData\": {\"04/13/2020\": {\"confirmed\": 66, \"confirmed_per_100k\": 8.71, \"deaths\": 0, \"deaths_per_100k\": 8.71}}, \"regionInfo\": {\"_id\": 4, \"lat\": -30.7546652, \"long\": -70.9005536, \"population\": 757586}}, \"5\": {\"region\": \"Valparaíso\", \"regionData\": {\"04/13/2020\": {\"confirmed\": 273, \"confirmed_per_100k\": 15.03, \"deaths\": 2, \"deaths_per_100k\": 15.03}}, \"regionInfo\": {\"_id\": 5, \"lat\": -33.0458456, \"long\": -71.6196749, \"population\": 1815902}}, \"6\": {\"region\": \"O’Higgins\", \"regionData\": {\"04/13/2020\": {\"confirmed\": 48, \"confirmed_per_100k\": 5.25, \"deaths\": 0, \"deaths_per_100k\": 5.25}}, \"regionInfo\": {\"_id\": 6, \"lat\": -34.1708298, \"long\": -70.7444382, \"population\": 914555}}, \"7\": {\"region\": \"Maule\", \"regionData\": {\"04/13/2020\": {\"confirmed\": 141, \"confirmed_per_100k\": 13.49, \"deaths\": 3, \"deaths_per_100k\": 13.49}}, \"regionInfo\": {\"_id\": 7, \"lat\": -35.5972284, \"long\": -71.48868, \"population\": 1044950}}, \"8\": {\"region\": \"Biobío\", \"regionData\": {\"04/13/2020\": {\"confirmed\": 512, \"confirmed_per_100k\": 32.89, \"deaths\": 2, \"deaths_per_100k\": 32.89}}, \"regionInfo\": {\"_id\": 8, \"lat\": -37.3391407, \"long\": -72.4106825, \"population\": 1556805}}, \"9\": {\"region\": \"Araucanía\", \"regionData\": {\"04/13/2020\": {\"confirmed\": 795, \"confirmed_per_100k\": 83.05, \"deaths\": 17, \"deaths_per_100k\": 83.05}}, \"regionInfo\": {\"_id\": 9, \"lat\": -38.6710116, \"long\": -72.2564576, \"population\": 957224}}}";
















const parsedJson = JSON.parse(json);

//Una vez teniendo el JSON parseado puedes acceder a las propiedades de este
//directamente usando un . seguido de la propiedad asi:

console.log(parsedJson[1].regionData["04/13/2020"].confirmed);

Si deseas obtener todas las propiedades confirmed en un array deberas usar un ciclo dentro de un ciclo, por ejemplo un ciclo for in, no te explicare como hacerlo deberás investigar y pensar en ello amigo, ya te di una parte, debes pensar en la otra parte, consideralo una forma de ayudarte a mejorar, mejoramos atravez de equivocarnos y experimentar, intenta obtener todas las propiedades confirmed de ese objeto en un array, se que puedes, yo ahí te obtuve la primera como ejemplo.

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.