eclipseIzHere
eclipseIzHere

Reputation: 83

How to format object output

When I print this out in my webpage:

var computer = {
  "Home":{
    "Desktop":{
        value:"Result"
    },
    "Documents":{
    },
    "Downloads":{
    },
    "Library":{
    },
    "Movies":{
    },
    "Music":{
    },
    "Pictures":{
    },
    "Library":{
    },
    "Downloads":{
    },
    "Files":{
    },
    "Devices":{
    },
    "USB":{
    },
    "Hard_Drive":{
    }
  },
}

i get this:

{"Desktop":{"value":"Result"},"Documents":{},"Downloads":{},"Library":{}," //blah blah blah

how do I format it so the output looks like the original code? aka the first piece of code

Upvotes: 0

Views: 59

Answers (1)

synthet1c
synthet1c

Reputation: 6282

You can use JSON.stringify(jsonobj, null, 2) if you want to do it dynamically from javascript, The third argument tells javascript how many spaces to use when pretty printing the json obj.

varcomputer={"Home":{"Desktop":{value:"Result"},"Documents":{},"Downloads":{},"Library":{},"Movies":{},"Music":{},"Pictures":{},"Library":{},"Downloads":{},"Files":{},"Devices":{},"USB":{},"Hard_Drive":{}}}

document.querySelector('pre').innerHTML = JSON.stringify(varcomputer, 0, 2)
<pre></pre>

Upvotes: 1

Related Questions