12

I have a simple Angular 4 application which is contacting a HTTP REST server and this server is simply returning a JSON payload and I would like to display this JSON payload as is in the browser. This is my makeRequest typescript function:

import { Component, OnInit } from '@angular/core';
import {Http, Response} from '@angular/http';

@Component({
  selector: 'app-simple-http',
  templateUrl: './simple-http.component.html'
})
export class SimpleHttpComponent implements OnInit {
  data: string;
  loading: boolean;

  constructor(private http: Http) {
  }

  ngOnInit() {
  }

  makeRequest(): void {
    this.loading = true;
    this.http.request('http://jsonplaceholder.typicode.com/posts/1')
    .subscribe((res: Response) => {
      this.data = res.json();
      this.loading = false;
    });
  }
}

The call to http://jsonplaceholder.typicode.com/posts/1 returns me the following JSON:

{
  "userId": 1,
  "id": 1,
  "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
  "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}

I now display this in my html component as:

<h2>Basic Request</h2>
<button type="button" (click)="makeRequest()">Make Request</button>
<div *ngIf="loading">loading...</div>
<pre>Data Obtained is: {{ data }}</pre>

But in the browser, I see this:

enter image description here

How do I make my JSON displayed as is?

2 Answers 2

32

You can use the json pipe. In your template:

<pre>Data Obtained is: {{ data | json }}</pre>

Also you have to change the type of your data property to any instead of string.

Sign up to request clarification or add additional context in comments.

7 Comments

This still does not display me the whole JSON!
I still get to see only this: Data Obtained is: {"id":1}
What if you do a console.log(this.data), does it contain the whole json you expect or just what gets displayed in the view?
Where do I put this console.log(this.data),? In my typescript file?
Yes, right after you do this.data = res.json();. Then you have to look at your browser's console window at runtime.
|
6

You have two options:

  • Use the built-in pipe JsonPipe (this.data should be of type any):

    <pre>Data Obtained is: {{ data | json }}</pre>

  • Get the JSON string manually:

    this.data = JSON.stringify(res.json()); //data is a string :)

or

 `<pre>Data Obtained is: {{ JSON.stringify(data) }}</pre>`

You have to understand that any value in a template is shown via calling its .toString() method, so a basic object (something like {key: value}) just shows [object Object]

4 Comments

Data Obtained is: {"id":1}
Strangely I do not see the whole JSON message!
Have you got any clues?
It means that that's what you have, there is no reason to show only part of an object. Check with your browser developer tool the network label

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.