I want to add a newline after each key value pair in an object using JSON.stringify()
In my actual code I will be getting a lot of key value pairs and having the new lines makes it easier to read.
Example code is:
let test = {'key' : ['one', 'two', 'three'], 'keyTwo' : ['one', 'two', 'three']}
let testOne = JSON.stringify(test, null, '\t')
console.log(testOne)
outputs:
{
"key": [
"one",
"two",
"three"
],
"keyTwo": [
"one",
"two",
"three"
]
}
I want:
{
"key": [
"one",
"two",
"three"
],
// <----- newline here
"keyTwo": [
"one",
"two",
"three"
]
}
I have tried
let test = {'key' : ['one', 'two', 'three'] "\\n", 'keyTwo' : ['one', 'two', 'three'] "\\n"}
let testOne = JSON.stringify(test, null, '\t\n')
Neither work
CodePudding user response:
let test = {'key' : ['one', 'two', 'three'], 'keyTwo' : ['one', 'two', 'three'], 'keyThree' : ['one', 'two', 'three']}
let testOne = JSON
.stringify(test, null, "\t")
.replaceAll(
"],\n\t\"",
"],\n\n\t\""
);
console.log(testOne);