javascript - Add newline after each key, value using JSON.stringify() - Stack Overflow

admin2025-04-19  0

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

Share Improve this question edited Feb 12, 2022 at 12:24 asked Feb 12, 2022 at 11:57 user18188599user18188599 6
  • The result of ['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
  • Please fix the format of your question. "outputs:" and "I want:" are not code and therefor shouldn't be formatted as such. – Andreas Commented Feb 12, 2022 at 12:05
  • 1 Why do you want the object as string in the first place? Isn't the collapsible view and the syntax highlighting from the console way easier to handle and grasp then just a plain piece of string? – Andreas Commented Feb 12, 2022 at 12:07
  • @Andreas, I have fixed the format and I end up sending the output to a txt file not the console – user18188599 Commented Feb 12, 2022 at 12:32
  • Then add the newline, if you insist on it, in the editor/viewer of your choice. Messing with the format of JSON will only produce errors later on. – Andreas Commented Feb 12, 2022 at 12:36
 |  Show 1 more ment

1 Answer 1

Reset to default 3

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);

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745062997a282813.html

最新回复(0)