0

How can I format the json output file to format the content like in the example?

[
  {"keyA1": "value1", "keyA2": "value2", "KeyAN": "valueN"},
  {"keyB1": "value1", "keyB2": "value2", "KeyBN": "valueN"},
  {"keyC1": "value1", "keyC2": "value2", "KeyCN": "valueN"},
]

There are a ton of answers like this other answer but this is not how I want it.

EDIT: maybe the question is not clear to everyone. I want the json file to be formated like this, not the javascript.

EDIT 2: this is how I am doing it now:

    const newAdmin = { 
        id: Math.floor(dateNow.getTime() / 1000), 
        firstName: firstName, 
        lastName: lastName, 
        birthDate: new Date(birthDate).toLocaleDateString(),
        registrationDay: dateNow.toLocaleDateString()
    };
    members.push(newAdmin);

    await Deno.writeFile(membersFilePath!, encoder.encode(JSON.stringify(members, null, 1)));
4
  • Where are you outputting it to? The console or a text file? Commented Dec 11, 2022 at 15:33
  • to a text file, more specific to a json Commented Dec 13, 2022 at 15:02
  • If you don't mind me asking, what is the added benefit of formatting your JSON this way? Do you need it to be human readable? If so, have you considered reading the JSON and presenting it in a readable fashion with the browser? Commented Dec 15, 2022 at 9:06
  • just personal preference Commented Dec 15, 2022 at 14:27

2 Answers 2

2

You can try mapping through each item individually and stringifying, then joining together by a linebreak:

var arr = [{"keyA1": "value1", "keyA2": "value2", "KeyAN": "valueN"},{"keyB1": "value1", "keyB2": "value2", "KeyBN": "valueN"},{"keyC1": "value1", "keyC2": "value2", "KeyCN": "valueN"},]

const result = "[\n" + arr.map(e => '  ' + JSON.stringify(e)).join(',\n') + "\n]";

console.log(result)

Sign up to request clarification or add additional context in comments.

2 Comments

I don't know what I am doing wrong but this is the output in the json file: "[\n {\"id\":1670713260,\"user\":\"new user\",\"city\":\"berlin\",\"registrationDay\":\"12/11/2022\"}\n]"
-1

How about a manual way of doing it. Check the code below:

const data = [{"keyA1": "value1", "keyA2": "value2", "KeyAN": "valueN"},{"keyB1": "value1", "keyB2": "value2", "KeyBN": "valueN"},{"keyC1": "value1", "keyC2": "value2", "KeyCN": "valueN"},];

        let json = "[";

        for (const item of data) {
            json += "\n  {";

            for (const key in item) {
                json += `"${key}": "${item[key]}",`;
            }

            json += "},";
        }

        json += "\n]";

        console.log(json);

6 Comments

This is not OP was asking for, Please re-read the question.
Edited the question to make the answer output exactly as the OP requested.
@Tinaria, check out this answer.
the json file is not coming out in the right format. I have no idea what I am doing wrong. I read the json file, push a new object into it. and that new array I am trying to write. but the format is not as desired
How are you outputting to the file, share your code.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.