Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pretty Json object in html

Tags:

html

angular

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>
like image 603
Shailesh Ladumor Avatar asked Jul 22 '17 09:07

Shailesh Ladumor


3 Answers

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>
like image 132
CodeWizard Avatar answered Nov 13 '22 19:11

CodeWizard


You can try with |json

<div class="box box-default">
    <code>
        <pre>{{placeDetail |json}}</pre>
    </code>
</div>
like image 22
Ketan Akbari Avatar answered Nov 13 '22 18:11

Ketan Akbari


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>
like image 1
Kaps Avatar answered Nov 13 '22 20:11

Kaps