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:
- It replaces all the instances of
:, whereas I need to replace the ones in front of key names only (like key:value) - Though replacement of
nulltonilis working, ifprofessionwas"profession": "null blah", it would make it"profession">"nil blah". I don't want to modify if the value is notnull.
Is there a better way to handle above scenarios?