0

In my app I have an API call that returns a JSON with the following (simplified) structure:

{
   "prop1": "foo",
   "prop2": "bar",
   "details": "{\"title\": \"Sample Konfabulator Widget\", \"name\": \"main_window\", \"width\": 500, \"height\": 500}"
}

The details prop contains a large stringified JSON that is read from the database.

Now, in the User Interface I want to display a prettified and formatted version of the details prop, something like:

enter image description here

I've tried it with:

<pre>{{ apiResponse.details }}</pre>

But the entire JSON is displayed in a single line.

The code I've tried is: https://stackblitz.com/edit/angular-pkfgvf?file=src%2Fapp%2Fapp.component.ts

Thanks!

2

2 Answers 2

1

Your details it's not a json object and that's why it's not being parsed, you will need to wrap it in JSON.parse()

details: JSON.parse("{\"title\": \"Sample Konfabulator Widget\", \"name\": \"main_window\", \"width\": 500, \"height\": 500}")

and you can now display it using the json pipe

{{ apiResponse.details | json  }}

Updated Demo

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

2 Comments

I've put it in a try / catch block, just to make sure that the app doesn't break: stackblitz.com/edit/…
@Juan - After parsing the JSON string, the resulting object is displayed with {{ apiResponse.details | json }}. I suggest mentioning it in the answer.
0

If you don't want to use JSON.parse() in you object directly, so you can create pipe that convert string object in to json.

Comments

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.