145

I am making an AngularJS 2 application with the beta version. I want to show a JSON representation of an object in my page, but it shows [Object Object] and not {key1:value1 ....}

From the component I can use:

get example() {return JSON.stringify(this.myObject)};

and then in the template:

{{example}}

but if I have an array of objects and would like to print a list of these objects, how I can do it?

Using:

<ul>
   <li *ngFor="#obj of myArray">{{obj}}</li>
</ul>

results in something like:

- [Object Object]
- [Object Object]
- [Object Object]
- [Object Object]

and so on. Is there a way to display those as JSON?

foralobo
  • 3,947
  • 5
  • 18
  • 17

9 Answers9

224

If you want to see what you you have inside an object in your web app, then use the json pipe in a component HTML template, for example:

<li *ngFor="let obj of myArray">{{obj | json}}</li>

Tested and valid using Angular 4.3.2.

LJH
  • 7,444
  • 3
  • 10
  • 19
Vlado Tesanovic
  • 6,369
  • 2
  • 20
  • 31
92

We can use angular pipe json

{{ jsonObject | json }}
ganesh kalje
  • 3,014
  • 2
  • 16
  • 12
49

To loop through JSON Object : In Angluar's (6.0.0+), now they provide the pipe keyvalue :

<div *ngFor="let item of object| keyvalue">
  {{ item.key }} - {{ item.value }}
</div>

DO READ ALSO

To just display JSON

{{ object | json }}
Vivek Doshi
  • 56,649
  • 12
  • 110
  • 122
  • 2
    Fav answer because it has both, and you'll likely need both: "just json" for simple variables. But for anything like a form control that has circular references, you'll need the loop presented as the first option. Both are still correct syntax in Angular9. – MotoRidingMelon May 13 '20 at 00:01
12

Dumping object content as JSON can be achieved without using ngFor. Example:

Object

export class SomeComponent implements OnInit {
    public theObject: any = {
        simpleProp: 1,
        complexProp: {
            InnerProp1: "test1",
            InnerProp2: "test2"
        },
        arrayProp: [1, 2, 3, 4]
    };

Markup

<div [innerHTML]="theObject | json"></div>

Output (ran through a beautifier for better readability, otherwise it is output in a single row)

{
  "simpleProp": 1,
  "complexProp": {
    "InnerProp1": "test1",
    "InnerProp2": "test2"
  },
  "arrayProp": [
    1,
    2,
    3,
    4
  ]
}

I have also discovered a JSON formatter and viewer that displays larger JSON data more readable (similar to JSONView Chrome extension): https://www.npmjs.com/package/ngx-json-viewer

<ngx-json-viewer [json]="someObject" [expanded]="false"></ngx-json-viewer>
Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
6
<li *ngFor="let obj of myArray">{{obj | json}}</li>
Jaydeep Kataria
  • 817
  • 10
  • 22
5

There are 2 ways in which you can get the values:-

  1. Access the property of the object using dot notation (obj.property) .
  2. Access the property of the object by passing in key value for example obj["property"]
Harkirat Saluja
  • 7,768
  • 5
  • 47
  • 73
2

Updating others' answers with the new syntax:

<li *ngFor="let obj of myArray">{{obj | json}}</li>
anacampesan
  • 105
  • 10
1
this.http.get<any>('http://192.168.1.15:4000/GetPriority')
  .subscribe(response => 
  {
  this.records=JSON.stringify(response) // impoprtant
  console.log("records"+this.records)
  });
  • 4
    Hey, to improve on what you wrote, you could add some explanation to your answer to make sure that a reader who is not very familiar with the concepts can understand your decisions. – AplusKminus May 17 '19 at 07:08
0

if you have array of object and you would like to deserialize them in compoent

get example() { this.arrayOfObject.map(i => JSON.stringify (i) ) };

then in template

<ul>
   <li *ngFor="obj of example">{{obj}}</li>
</ul>
Arash
  • 3,458
  • 7
  • 32
  • 50