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
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
['one', 'two', 'three'] + "\\n"
is the string 'one,two,three\\n'
. It's not an array anymore. JSON.stringify(test, null, '\t\n')
adds a newline after each key/value pair.
– jabaa
Commented
Feb 12, 2022 at 12:00
const test = {'key' : ['one', 'two', 'three'], 'keyTwo' : ['one', 'two', 'three'], 'keyThree' : ['one', 'two', 'three']}
const testOne = JSON
.stringify(test, null, "\t")
.replaceAll(
"],\n\t\"",
"],\n\n\t\""
);
console.log(testOne);