What is the best way to cycle through objects rather than arrays?

Is it possible to retrieve JSON data from the following URL ? Specifically, I am interested in getting information on objects such as "uf", "ivp", and so on to display them in a table or list. The challenge is that this data is not presented in an array format. How can I iterate over these objects?

{
    "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
    }
}

I want to iterate through each object like "uf", "ivp", etc. Is there a way to achieve this even though it's not in array form?

This is the method for my service:

search(): Observable<Object> {
    const queryUrl = this.apiUrl;
    return this.http.get(queryUrl);
  }

Answer №1

It is important to properly handle the JSON object received from an HTTP GET request by parsing it and treating it as an object. This allows you to easily access each member of the JSON object.

this.retrieveData().subscribe(response => {
    let jsonData: any = JSON.parse(response);
    console.log(jsonData);
    // loop through jsonData members for further processing
});

Answer №2

Loop through the response object and extract only the values that are considered objects.

for(const property in responseObject) {
    if(typeof responseObject[property] === 'object') {
        console.log(responseObject[property]);
    }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unable to successfully send a post request using cUrl while attempting to read data from a file

Hey, I'm facing an issue with sending a jSon file using the post command. I am currently using curl version 7.54.0 on Windows. Here is the command I tried to run: E:\Pobrane Na Big Data\cURL\curl-7.54.0-win64-mingw\bin>curl -v ...

What could be the reason for passport.serializeUser function not being available?

After spending nearly an hour searching, I am still unable to pinpoint the issue in my code. Not only have I failed to find a solution online, but reaching out for help in coding communities on Discord has also been futile as no one seems to know why this ...

What is the best approach: Developing a class or a service to handle multiple interfaces?

My current project involves creating multiple interfaces, and I would like to keep them separate for clarity. Would it be wise to do this in a service or a class? Additionally, is it possible to utilize dependency injection for classes? Thank you for your ...

Create a JSON structure with nested elements

I've been attempting to create a nested JSON from a simple JSON but haven't been successful yet. Here is the JSON structure I'm trying to convert: [ { "station_id": "ESP0001", "datetime": "2022-1 ...

Transforming Java objects into JSON using Jackson library

Here's the JSON format I'm aiming for: { "information": [{ "timestamp": "xxxx", "feature": "xxxx", "ean": 1234, "data": "xxxx" }, { "timestamp": "yyy", "feature": "yyy", "ean": 1234 ...

Using JSON in combination with any client or server programming language allows for seamless

Is it possible to use any language as server and client sides if I choose JSON as the data exchange format? I am currently working on a web application using PHP and jQuery. My server delivers responses in JSON format, and now I want to develop an Android ...

Testing Angular Service Calls API in constructor using Jasmine Test

My service is designed as a singleton, and its constructor initiates an API call function. This simplifies the process during service initialization, reducing the complexity and dependencies on various components like AppComponent to import and execute API ...

Unable to perform type casting in Typescript

I recently dived into the world of TypeScript by picking up a book titled Typescript Revealed (Published in February 2013). Chapter 2 caught my attention with a section on "Casts" featuring an intriguing example: var a : int = <int>SomeNumberAsAStri ...

Formatting the Return Values of Ionic Select

Having an issue with Ionic 3. Using an <ion-select> element with ngModel="x". When attempting to display the value in the console, it shows with extra spaces and line breaks. I tried replacing line breaks with 'e' and spaces with 'a&ap ...

ngModel displays the previously selected value in the md-select

Whenever I use console.log to display the value of selectedVendor in the method vendorUpdate, it is showing the previous value of selectedVendor rather than the new one. <div> <md-select id="vendorVariable" class="vm-select-wrap" ...

Configure Jest to run tests for Lit using TypeScript with Microbundle

Today, I began the process of migrating our web components to lit-html. However, when attempting to run the initial test, an error message was encountered: SyntaxError: Cannot use import statement outside a module > 1 | import {LitElement, html, css, s ...

Discovering errors within a reactive form in Angular using a loop to iterate through the FormArray

I am currently utilizing Angular CLI version 16.2.1. As I progress through a course, I am working with reactive forms. In a list of 'Recipes', I aim to include a Recipe with various inputs and a collection of ingredients. However, I am encounter ...

unable to retrieve the values of the rowdata in GridOption

ngOnInit(): void { this.fetchAllAnimeReviews(); } public fetchAllAnimeReviews(){ this.animeservice.getAllAnimeReviews() .subscribe( response => { this.Anime = response; this.gridOption = this.createGridO ...

Is there a way to program a function that can automatically trigger or refresh an HTTP POST method?

How can I create a method in a distant component that will run a POST request when a button is clicked? I believe I need to use a service in this situation. It's not necessary for it(this.qwe) to be in the constructor, it's just an example... ...

Different varieties of TypeScript's keyof when working with objects

I am grappling with the concept of TypeScript's types when incorporating the keyof type operator on objects. Check out this example: type TypeA = { [k: number]: boolean }; type AKey = keyof TypeA; // ^? type AKey = number type TypeB = { [k: string] ...

Navigating through alterations in intricate data utilizing the BehaviorSubject concept

If I have a Service that offers a BehaviorSubject to any component needing the most up-to-date version of certain data, how can these components differentiate what changed in the data when they receive notifications? export class DataService { pri ...

When utilizing gapi (typescript) to send emails, the Sent box may contain incorrectly encoded email messages

When I send emails using gapi, the receiver receives them correctly. However, when I check my "Sent" box, the emails appear as Chinese gibberish characters like in this image. This issue only occurs when using non-gmail applications. If I view the Sent bo ...

What is the best way to parse a nested json array in PHP?

I am facing an issue with a complex JSON structure that I need to deserialize: [ {"product": {"category_id":1, "created_at":"2015-06-13 17:49:58", "description":"CF77 COIN FINDER", "url_image":"IMG_ ...

Problems with lazy loading routes in Angular 2+: routes not loading without any error messages

I've been attempting to incorporate lazy loading in my Angular routes. Despite having a functional version in another application, my current implementation does not seem to be working. There are no visible errors, just an empty <router-outlet>. ...

The parameter type 'string | null' cannot be assigned to the value function 'ValueFn<SVGPathElement, Datum[], string | number | boolean | null>'

I recently delved into the world of d3 and attempted to create a simple line chart using d3, TypeScript, and react. However, I keep encountering a TypeScript error whenever I try to implement it. Strangely, I can briefly see my chart before it disappears a ...