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);
  }
1

2 Answers 2

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
});
Sign up to request clarification or add additional context in comments.

Comments

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]);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.