Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON Stringify Ouput to Multiple Lines

I'm writing a simple JS app that takes in a JSON file, displays the information to the user and allows them to modify it, and then allows them to re-export the JSON. However, the JSON that is being brought in is multi-line; each key/value is on its own line. When I use .stringify to output the JSON, it all appears on one line. Is there any way for the stringify method to separate the lines?

JSON Structure:

{"Title":
 {"lvlOne":[
  {"key":"val"},
  {"key":"val"},
  {"key":"val"}
 ],
 "lvl2":[
  {"key":"val"},
  {"key":"val"},
  {"key":"val"}
 ]}
}

But when I output, it all shows:

{"Title":{"lvlOne":[{"key":"val"},{"key":"val"},{"key":"val"}],"lvl2":[{"key":"val"{"key":"val"},{"key":"val"}]}}
like image 743
Joey N Avatar asked Jul 01 '14 17:07

Joey N


1 Answers

You can use the space parameter of the stringify method. From the official page, here is the relevant excerpt:

JSON.stringify({ a: 2 }, null, " ");   // '{\n "a": 2\n}'
like image 67
Mike Pelley Avatar answered Sep 24 '22 01:09

Mike Pelley