How to save JSON values in a variable/array after subscribe?

json array of objects
save data in json file using angular 6
how to get data from json object in angular 6
json array of arrays
display json data in html table using angular 4
json array example in java
how to read json file in angular 7
angular observable array

I'm receiving JSON files that look like this:

{streetAddress: "Kosterlijand 20", postalCode: "3980", city: "Bunnik", country: "Netherlands"}

The files have different length but the structure will always be

{key: "string value", key: "string value", etc...}

I'm trying to subscribe to get that data, but no matter how I try to subscribe, I cannot store the data in any way. If I only use this:

this.service.getEntityDetails()
        .subscribe(data => console.log(data));

I can see in the console:

{streetAddress: "Kosterlijand 20", postalCode: "3980", city: "Bunnik", country: "Netherlands"}

But if I try anything else, for example this:

    public test: any[];
    public test1: any[]=[];
    public test2: string[];
    public test3: string[]=[];
    public test4: {};

    constructor(service:Service){}
    this.service.getEntityDetails()
    .subscribe(data => {
        test=data;
        test1=data;
        test2=data;
        test3=data;
        test4=data;

        console.log("test:"+test+", test1: "+test1+", test2: "+test2+", test3: "+test3);
    });
 }

The output in console is always the same:

test:[object Object], test1: [object Object], test2: [object Object], test3: [object Object] test4[object Object]

I would like to make it work the way KeyValuePipe works.

But because I cannot save these values in any way, I cannot use the pipe. Can someone help me solve this?

I can provide more information if necessary.

So, to explain a few things...

public someRandomData: Array<any> = [];

  this.service.getEntityDetails()
    .subscribe(data => {
        someRandomData=data;
    });

That's it. You already successfully stored the value you wanted to get.

If you REALLY wanted to do a console log of your object in a string, you either refer to the fields the object has like...

console.log(this.someRandomData[0].fieldOfYourJSONObject)

Of you write a function that iterates through the keys of your objects. But since you only asked for storing your object, that should suffice.

Introduction to JSON, How do I add a JSON object to an array? @JanDvorak Yes, request executes asynchronously, if you need json data to proceed, you should call other code after setting myjson variable. As solution it could be like 'callbackFuncWithData' from Ravi answer OR just call another function. – webdeveloper Apr 2 '13 at 14:10

You could use async pipe instead of subscribing in the ts file. Using the example of @ak.leimrey and @Fateme Fazli

/** @file app.component.ts */ 

@Component({ /* ... */ })
export class AppComponent  {
    public test$: Observable<any[]>;

    constructor(service: Service) {
       this.test$ = this.service.getEntityDetails();
    }
}

In your template:

<ng-container *ngIf="(test$ | async) as test">
    <div *ngFor="let item of test | keyvalue">
        {{item.key}}:{{item.value}}
    </div>
</ng-container>

An Introduction to JSON, What is the correct way to write a JSON array? i am newbie in android Here is JSON defined, i need to manually store this JSON in String variable using JAVA in Android.I am getting diffculty to do this.here is the code i have tried but its not correct.

If you want to display data that comes from a datasource, it should be stored in a component property. Example:

/** @file app.component.ts */ 

@Component({ /* ... */ })
export class AppComponent  {
  public test: any[];

  constructor(service: Service) {
    this.service
      .getEntityDetails()
      .subscribe(data => {
        this.test = data;  
        console.log('My data', this.test);
      });
  }
}

Then you can display it like this:

<!-- @file app.component.html -->

<div>{{ test }}</div>

Observables Array Operations, Now every object in our array has "Dr." prepended to the value of its name property our subscribe callback gets invoked, the data it receives will be a list of JSON Since filter accepts an Observable , and not an array, we have to convert our  Hi @Eric_Hu, I have made a test on my side and please take a try with the following workaround: Add a proper trigger, here I use Flow Button trigger. Add a Variables-> “Initialize variable” action, Name set to J, Type set to Object and Values set to object that you provided.

JSON Array – REST API Tutorial, JSON array are ordered list of values. JSON array can store multiple value types. JSON array can store string, number, boolean, object or other array inside  Arrays in JSON are almost the same as arrays in JavaScript. In JSON, array values must be of type string, number, object, array, boolean or null. In JavaScript, array values can be all of the above, plus any other valid JavaScript expression, including functions, dates, and undefined.

Loading and Saving JSON data, The most convenient way to exchange or store data is in JSON format - the format that the and observable arrays, which are implemented as JavaScript functions and therefore Result: jsonData is now a string equal to the following value. Introduction of JSON in Python : The full-form of JSON is JavaScript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a built-in package called json. To use this feature, we import the json package in Python script.

Read an External JSON file in Angular 4 and Convert Data to HTML , I have declared an array named arrBirds of type string. I am adding the JSON data extracted from the file into an array, which I'll later bind with a <table> using *​  How to parse JSON in Java JSON (JavaScript Object Notation) is a lightweight, text-based, language-independent data exchange format that is easy for humans and machines to read and write. JSON can represent two structured types: objects and arrays.

Comments
  • @SuyashGupta Why should he? Unless he uses a version below Angular 5, there is NO need to map it to a json format.
  • Of course it will be always [object Object] because you try to output the object. That's not how console.log works in tandem with strings. You're a bit confused what happens.
  • console.log("test:"+test+"...) is wrong first off... it should be console.log("test:", test, "test1", test1...");
  • I think the user struggles on a very basic level. He's saving the value, but makes the basic mistake, that he doesn't refer to the objects with either their index or fields and thinks they're unreadable based on the way he constructed his console.log statement
  • Yes I answered maybe a bit too fast... Thank you !
  • No, it's a great response and shows how you can use the keyvalue pipe! The issue simply was "simpler" than that.