-2

I am calling an API it is returning the dynamic JSON, I want to just display this into the DIV or TEXTAREA.

JSON Can be : {"id":255,"Val2":"\n {"Id": 28, "deta": "\n {\n \"strategy1\":{\"$type\":\"Text1, text2\"}}"}"}

or

{"id":251,"name":"text", enable:true}

Expected result:


    {
      "id":255,
      "Val1":
       {
          Id: 28, 
          "deta": 
          {
            "strategy1":
            {
              "$type":"text1, text2"
            }
          }
       }
    }

mohd afzal
  • 13
  • 1
  • 7
  • 1
    Please fix the [format](https://stackoverflow.com/help/formatting) and show us what you've tried so far to solve this on your own. – Andreas Sep 21 '22 at 14:36
  • 1
    _"JSON Can be ... or ..."_ - Both of the examples are _not_ [JSON](https://www.json.org/json-en.html) – Andreas Sep 21 '22 at 14:38

1 Answers1

0

( yoinked from the following answer already )

https://stackoverflow.com/a/7220510/20053031

const obj = resultOfYourRequest;
const prettyString = JSON.stringify(obj, null, 2);

document.getElementById('your-element-id').innerHtml = prettyString;
  • I already used the above method like:`function output(inp) { document.body.appendChild(document.createElement('pre')).innerHTML = inp; } var obj = { "id": 255, "Val2": "\n {\"name\":\"test\"}" };; const prettyString = JSON.stringify(obj, null, 2); output(prettyString);` Result: `{ "id": 255, "Val2": "\n {\"name\":\"test\"}" }` but it does not format what I expected Result:`{ "id": 255, "Val2": { "name": "test" } }` – mohd afzal Sep 22 '22 at 07:20
  • The formatting of your comment does not give a clear picture of what went wrong here, what are you expecting? More indentation? – Alex Stringer Sep 22 '22 at 10:41