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

131
Views
Modify JSON string

Say if I have a object:

{ "name": "abc", "profession": null, "createdOn": "2022-01-05 10:00:05" },

when I convert it to a string, I need output like:

'{"name">"abc", "profession">nil, "createdOn">"2022-01-05 10:00:05" }

If I global replace like:

JSON.stringify(inputObject).replace(/null/g, 'nil').replace(/:/g, '>'),

I will get:

{"name">"abc", "profession">nil, "createdOn">"2022-01-05 10>00>05" }

There are couple of problems here:

  1. It replaces all the instances of :, whereas I need to replace the ones in front of key names only (like key:value)
  2. Though replacement of null to nil is working, if profession was "profession": "null blah", it would make it "profession">"nil blah". I don't want to modify if the value is not null.

Is there a better way to handle above scenarios?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

function format(inputObject) {
  if (inputObject) {
    if (typeof inputObject === "object" && !Array.isArray(inputObject)) {
      const objectEntries = Object.entries(inputObject);
      if (objectEntries.length) {
        const reformatted =
          objectEntries
            .map(([key, value]) => `"${key}">${!value ? 'nil' : "\"" + value + "\""}`)
            .join(",") || "";
        return `{${reformatted}}`;
      }
      return "{}";
    }
  }
  return `${inputObject}`;
}

Illustration

const obj = {
  "name": "abc",
  "profession": null,
  "createdOn": "2022-01-05 10:00:05"
};

function format(inputObject) {
  if (inputObject) {
    if (typeof inputObject === "object" && !Array.isArray(inputObject)) {
      const objectEntries = Object.entries(inputObject);
      if (objectEntries.length) {
        const reformatted =
          objectEntries
          .map(([key, value]) => `"${key}">${!value ? 'nil' : "\"" + value + "\""}`)
          .join(",") || "";
        return `{${reformatted}}`;
      }
      return "{}";
    }
  }
  return `${inputObject}`;
}

console.log(format(obj));


WYSIWYG => WHAT YOU SHOW IS WHAT YOU GET

about 4 years ago · Juan Pablo Isaza Report

0

The simple solution may be:

const a = {
  name: 'abc',
  gender: 'null',
  gender2: 'my null value',
  profession: null,
  createdOn: '2022-01-05 10:00:05',
};

const b = JSON.stringify(a)
  .replace(/":/g, '">')
  .replace(/">null/g, '">nil')
;


console.log(b);

Output:

{"name">"abc","gender">"null","gender2">"my null value","profession>nil,"createdOn
">"2022-01-05 10:00:05"}
about 4 years ago · Juan Pablo Isaza 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!