Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

167
Views
How to apply custom formatting for a JSON stringify?

I have the following code:

const sample = [
  {
    name: "apple",
    points: [ { x: 1, y: 2 }, { x: 3, y: 4 }, { x: 5, y: 6 }, { x: 7, y: 8 } ],
    age: 24
  },
  {
    name: "banana",
    points: [ { x: 1, y: 2 }, { x: 3, y: 4 }, { x: 5, y: 6 }, { x: 7, y: 8 } ],
    age: 45
  }
];

const qwer = JSON.stringify(sample, null, 2);

console.log(qwer);

If you run it, you'll notice it has nice formatting, except for the points array, which is extremely verbose.

I would like everything to be indented like normally (which is why I'm passing in 2 for the final parameter to stringify), but I would like the points array to only take a single line, like how it is declared in the code.

The reason for this is because currently each points array is stretched to like 18 lines, when there will only ever be 3 or 4 items. I would like them to stay on one line.

I tried to use a custom replacer, and while it somewhat worked, it forced the JSON array to be a string. But it's not a string. I want it to stay an array.

Is there any way to do this?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

For the general solution, a mini parser would be the best approach, but a quick but ugly-looking approach would be to use a replacer to replace arrays with stringified strings with a unique value as a prefix which can be replaced afterwards.

const sample = [
  {
    name: "apple",
    age: 24,
    points: [ { x: 1, y: 2 }, { x: 3, y: 4 }, { x: 5, y: 6 }, { x: 7, y: 8 } ],
  },
  {
    name: "banana",
    age: 45,
    points: [ { x: 1, y: 2 }, { x: 3, y: 4 }, { x: 5, y: 6 }, { x: 7, y: 8 } ],
  }
];

const withNestedStringifiedArrays = JSON.stringify(
  sample,
  (key, value) => key && Array.isArray(value) ? '@@UNIQUE@@' + JSON.stringify(value) : value,
  2
);
const output = withNestedStringifiedArrays.replace(
  /"@@UNIQUE@@(.*?)"(,)?$/gm,
  (_, stringifiedArr, possibleComma = '') => stringifiedArr.replaceAll('\\', '') + possibleComma
);
console.log(output);

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!