0

I am using Angular 8.

On my app.component.html I am displaying some json data linke this:

{{ myJsonData }}

This display is coming out like this:

{"information":"Information One","output":[{"ipaddress":"192.168.2.1","sName":"R45665"},{"ipaddress":"192.168.2.2","sName":"H4433D"}]}

But I need it to display like this:

{
    "information":"Information One",
    "output":[
        {
            "ipaddress":"192.168.2.1",
            "sName":"R45665"
        },
        {
            "ipaddress":"192.168.2.2",
            "sName":"H4433D"
        }
    ]
}

I've tried {{ myJsonData | json }}

But that just gives my lots of backslashes so makes the problem worse.

How can I do this?

2
  • its because its not json but a string use JSON.parse(myJsonData) Commented Dec 18, 2019 at 9:53
  • See answer to question, how to display JSON data: stackoverflow.com/questions/59862466/… Commented May 25 at 9:44

2 Answers 2

3

Try using <pre> tag,

<pre class="list-group-item-text"> {{ myJsonData | json }} </pre>

STACKBLITZ DEMO

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

1 Comment

This gives me this result: {\"information\":\"Information One\",\"output\":[{\"ipaddress\":\"192.168.2.1\",\"sName\":\"R45665\"},{\"ipaddress\":\"192.168.2.2\",\"sName\":\"H4433D\"}]}
0

you are trying to pretty print the json output. its the pre tag you need for this.

your response to the controller comes this way..

{"information":"Information One","output":[{"ipaddress":"192.168.2.1","sName":"R45665"},{"ipaddress":"192.168.2.2","sName":"H4433D"}]}

in your controller file lets assume its assigned to the response.

myJsonData = JSON.stringify(response);

in your html view you can then use this..

<pre class="~~"> {{ myJsonData }} </pre>

3 Comments

Can't do that...I get: Type 'string' is not assignable to type 'myJsonData[]'
okay maybe thats why you had to use the json pipe filter. because if you posted how you received the json to the variable. then i would have understood better.
The data is added using .push()

Your Answer

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