Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON Stringify not indenting properly in <pre> tag

I'm trying to use the JSON.stringify() function on a Javascript object but the indentation on the first curly brace doesn't look correct. The first line is indented, but everything else looks fine. I did some research, and another answer on StackOverflow suggested to use the <pre> tag. Ideas?

My javascript code:

var result = {
    name: "Jim Cowart",
    location: {
        city: {
            name: "Chattanooga",
            population: 167674
        },
        state: {
            name: "Tennessee",
            abbreviation: "TN",
            population: 6403000
        }
    },
    company: "appendTo",
};

$scope.data = JSON.stringify(result, null, 2);

My HTML code:

<pre content-load id="json-data">
    <div class="loader"></div>
    <span>{{ data }}</span>
</pre>

What actually renders in the browser (took a snapshot):

Snapshot of JSON Stringify

like image 544
HiChews123 Avatar asked Dec 01 '25 17:12

HiChews123


1 Answers

You have indentation before <span>, and <pre> means that this should be preserved in the rendering. Try:

<pre pams-content-load id="json-data">
    <div class="loader"></div>
    <span>
{{ data }}</span>
</pre>

so that the data is at the beginning of the line. Or put <pre> only around the JSON span.

<div class="loader"></div>
    <pre pams-content-load id="json-data"><span>{{ data }}</span></pre>
</pre>
like image 67
Barmar Avatar answered Dec 04 '25 06:12

Barmar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!