11

How do I pretty print JSON object in html? I tried this solution but not working. Please help me what I need to do to achieve this. I tried using json pipe:

<div class="box box-default">                     
    <pre>{{placeDetail |json}}</pre>                  
</div>
AT82
  • 67,510
  • 23
  • 129
  • 159
Shailesh Ladumor
  • 6,474
  • 4
  • 37
  • 51

3 Answers3

18

Try this:

    <div class="box box-default">
        <code>
            <pre>{{placeDetail |json}}</pre>
        </code>
    </div>
Ketan Akbari
  • 9,971
  • 5
  • 30
  • 51
6

For native JS: : <script>document.write(JSON.stringfiy(placeDetail )</script> For angular you need to add the <code> tag

Angular:

<code>
    <pre>{{placeDetail |json}}</pre>
</code>

Native JS:

<div class="box box-default">
    <pre><script>document.write(JSON.stringify(placeDetail))</script></pre>
</div>
CodeWizard
  • 110,388
  • 20
  • 126
  • 153
1

Angular2 solution:

https://plnkr.co/edit/msa5JDjPUxGMFcHptTu8?p=preview

javascript solution:

var placeDetail = {
    'a': {
        'b': 1,
        'c': 2
    },
    'd': 3
}; 
document.getElementById("json-result").innerHTML = JSON.stringify(placeDetail, undefined, 2);
<pre id="json-result">
</pre>
Kaps
  • 189
  • 7