0

I want to retrieve json data from this url http://mindicador.cl/api. But specifically I want to retrieve all objects like "uf", "ivp" and so on in order to then show them in a table or list. Unfortunately, there are no in array format so How can Iterate over there?

{
    "version": "1.5.0",
    "autor": "mindicador.cl",
    "fecha": "2018-10-24T17:00:00.000Z",
    "uf": {
        "codigo": "uf",
        "nombre": "Unidad de fomento (UF)",
        "unidad_medida": "Pesos",
        "fecha": "2018-10-24T04:00:00.000Z",
        "valor": 27413.56
    },
    "ivp": {
        "codigo": "ivp",
        "nombre": "Indice de valor promedio (IVP)",
        "unidad_medida": "Pesos",
        "fecha": "2018-10-24T04:00:00.000Z",
        "valor": 28523.73
    }
}

And I want to iterate through all objects, that is, "uf", "ivp", and so on. How can I do it? It's not an array so I can't use map I guess.

This is my service's method:

search(): Observable<Object> {
    const queryUrl = this.apiUrl;
    return this.http.get(queryUrl);
  }
The Head Rush
  • 3,157
  • 2
  • 25
  • 45

2 Answers2

1

You should parse the JSON object that your HTTP GET returns and treat it as an object so you can access each member of the JSON object.

this.search().subscribe( data => {
    let jsonObject: any = JSON.parse(data);
    console.log(jsonObject);
    // parse through jsonObject members
});
ceaserg
  • 106
  • 7
0

Iterate the response object, but select only those values which are objects.

for(const key of Object.keys(sampleResponseObject)) {
    if(typeof sampleResponseObject[key] === 'object') {
        console.log(sampleResponseObject[key]);
    }
}
gr4viton
  • 1,434
  • 1
  • 8
  • 21