0

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?

2
  • 1
    One way is to use Object.keys and to interate through each property. And you can keep a string variable where you push everything you want Commented Mar 12, 2022 at 11:41
  • 3
    It is rarely a good idea to modify valid JSON into invalid JSON. Why do you need this? Commented Mar 12, 2022 at 11:41

2 Answers 2

1

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"}
Sign up to request clarification or add additional context in comments.

1 Comment

I fixed for nil too. I just missed ". .replace(/">null/g, '">nil')
1
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

Comments

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.