3

This topic is addressed on this site for PHP and Rails but I don't see anything for standard JavaScript.

If my JSON object has a null value in a pair it looks like this id:null but I need it to look like this id:""

Since JSON doesn't support single quotes I'm unsure how to do this.

Current Result

{"id":"e6168d55-1974-e411-80e0-005056971214","label":"List","parentId":null}

Desired Result

{"id":"e6168d55-1974-e411-80e0-005056971214","label":"List","parentId":""}
GoBeavs
  • 499
  • 2
  • 10
  • 25
  • Can you post some sample json, i.e. where the `id:null` fits into the overall json object? And is this json as a string, or a Javascript object? – Rhumborl Dec 04 '14 at 20:26
  • Loop through your object and replace all instances of null with empty strings *before* converting to JSON. – JCOC611 Dec 04 '14 at 20:26
  • How are you building this JSON? Are you calling `JSON.stringify` on a JavaScript object (and that object has a null value that you wish to express as an empty string in the JSON output)? Or are you trying to do a JSON-to-JSON transform? – apsillers Dec 04 '14 at 20:30
  • Just recursively change all the `null` to `""` after converting the `JSON` to an `object`, then convert back to `JSON`. See [this](http://stackoverflow.com/a/722732/1305067) for how to recursively transverse an object. – paulvs Dec 04 '14 at 20:35

7 Answers7

11

JSON.stringify accepts a replacer callback that allows you to substitute values with other values. The replacer callback runs for every key-value pair processed in the input, and it replaces the supplied value with it return value.

Simply have your replacer callback look for any null values and replace them with the empty string:

var myObj = {
               "id":"e6168d55-1974-e411-80e0-005056971214",
               "label":"List",
               "parentId":null
             };

JSON.stringify(myObj, function(key, value) {
    // if value is null, return "" as a replacement
    if(value === null) {
        return "";
    }

    // otherwise, leave the value unchanged
    return value;
});

If you don't have an object, but only have JSON as input, you can build an object using var myObj = JSON.parse(jsonInput);

apsillers
  • 112,806
  • 17
  • 235
  • 239
0

Not sure why you're mentioning single quotes, but in json it would never look like this:

id:null

Perhaps you meant this?

{
  "id": null
}

Regardless, if you want to encode "" instead of null, you provide an empty string, because "" is simply how you encode an empty string. So go through your nulls and replace them with empty strings.

Evert
  • 93,428
  • 18
  • 118
  • 189
0

I do not believe there is a quick and easy way to map NULL to "". If you are not working with a very deep JSON structure then you can iterate through the structure and change it from NULL to "".

cpisano
  • 11
  • 1
0

This solved my issue

  myJSON = myJSON.replace(/null/i, "\"\"");
GoBeavs
  • 499
  • 2
  • 10
  • 25
  • LOL, you posted the same answer as I was typing :) – paulscode Dec 04 '14 at 20:36
  • 3
    Try it with `var myJSON = JSON.stringify(["This nullifies the answer"])`. – Oriol Dec 04 '14 at 20:46
  • you can improve your answer like this: `myJSON = myJSON.replace(/: null,/i, ":\"\",");` but there are probably still cases where this is not recommendable because it might still lead to errors. – shaedrich Mar 26 '21 at 14:04
0

Try this.

var response = yourResponseText.replace(/null/g, '')
var json = JSON.parse(response);

Replace null in your response text with '' . After replacing, parse response string to JSON.

Chetan
  • 66
  • 9
  • **Warning:** That replaces `null` with **nothing**. That will make your JSON invalid! If someone wants to use this approach, check out @gobeavs [answer](https://stackoverflow.com/a/27303519/7451109) – shaedrich Mar 26 '21 at 14:02
-1

Try this

const obj = {
    "id": "e6168d55-1974-e411-80e0-005056971214",
    "label": "List",
    "parentId": null
}

const jsonObj = JSON.stringify(obj,
    (key, value) => (value === null) ? '' : value
);

console.log(jsonObj);
// return -> {"id":"e6168d55-1974-e411-80e0-005056971214","label":"List","parentId":""}
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 05 '22 at 09:45
-3
{"id":"e6168d55-1974-e411-80e0-005056971214","label":"List","parentId": "parentId" ? "parentId" : ''}
Syscall
  • 19,327
  • 10
  • 37
  • 52
  • 1
    Thank you for this code snippet, which might provide some limited, immediate help. A [proper explanation](https://meta.stackexchange.com/q/114762/9193372) would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you’ve made. – Syscall Mar 26 '21 at 12:57
  • Thank you for your answer but I can't see how this is solving the problem. Your ternary statement will always set "parentId" to "parentId". – shaedrich Mar 26 '21 at 14:06
  • 1
    Always explain your answer. – Arvind Kumar Avinash Mar 26 '21 at 14:14