0

I am working on a JSON file generator while everything works fine the final result is not as good as expected

With following stringify function call, following json file would be generated.

var jsonString = JSON.stringify(object, null, 2);
{
  "key_1": 2397,
  "key_2": "text1",
  "key_3": "text",
  "key_4": [
    "12345",
    "23456"
  ],
  "key_5": [
    0,
    1
  ],
  "key_6": [
    0
  ],
  "key_7": [
    "https://xxxx",
    "https://xxxx"
  ],
  "key_8": "12345",
}

In the JSON file I want to make key_5 and key_6 skip the stringify attributes and make it like this

  "key_5": [0,1],
  "key_6": [0],

So the final file would look like this

{
  "key_1": 2397,
  "key_2": "text1",
  "key_3": "text",
  "key_4": [
    "12345",
    "23456"
  ],
  "key_5": [0,1],
  "key_6": [0],
  "key_7": [
    "https://xxxx",
    "https://xxxx"
  ],
  "key_8": "12345",
}

I think it is possible to customized the rule for certain key but I am very new to JS so I am not sure how to do so...

    jsonString = JSON.stringify(object, (key, value) => {
      if (key === "key_5"||key === "key_6") return Do_something_ELSE;
      return value;
    }, 2);
Shawn
  • 5
  • 4
  • I don't know how to do it, but I really want to know why would you need this – theemee Dec 06 '22 at 15:01
  • There is no functional difference between an array formatted with line breaks and one without (in JSON), what exactly are you trying to achieve this for? – DBS Dec 06 '22 at 15:01

0 Answers0