delliottg
delliottg

Reputation: 4140

How to include nulls in JSON object as placeholders?

My project will use an HTML 5, & jQuery Mobile front end, and pass JSON data via AJAX back & forth to a C# ASP.NET backend that talks to a MySQL database (the example below doesn't show any backend C#, MySQL or AJAX code yet).

I've figured out what my JSON object (string?) is going to look like, here's a simplified version to save space:

{
    "Events": {
        "Common": {
            "User": "string",
            "PCBID": 52759,
            "Notes": "string"
        },
        "Assign": {
            "SBEPN": "string",
            "SBEJobNumber": "string",
            "SBESerialNumber": "string",
            "SBEModelNumber": "string"
        }
    }
}

For each of the Events, the Common node will always be populated, and depending on the Event Type (Assign, Create, Modify, etc.) some of the data will be populated, and the rest of the available variables will not. For brevity, I've only included the Assign node, but there are five other somewhat more complex nodes below it.

I thought I could pre-assign the variables in JS to either null or JSONObject.NULL and then use a ternary statement to assign it it's true value if supplied by the user, or leave it null. The idea being that I don't have to have a bunch of overloads in the c# code behind, I can just pass in a big blob of JSON and figure out what to do with it based on what's been populated or left null. Here's one way I've tried to declare the variables:

var user = JSONObject.NULL;
var pcbid = JSONObject.NULL;
var notes = JSONObject.NULL;
etc...

These show up as JSONObject undefined, and if I instead declare them as null, like this:

var user = null;
var pcbid = null;
var notes = null;
etc... 

they show up as undefined instead of null whether I JSON.stringify them or not.

This is how I'm assigning strings to the variables:

user = $("#input_common_user").val() !== null ? $("#input_common_user").val() : JSONObject.NULL;
pcbid = $("#input_common_pcbid").val() !== null ? $("#input_common_pcbid").val() : JSONObject.NULL;
notes = $("#input_common_notes").val() !== null ? $("#input_common_notes").val() : JSONObject.NULL;

Here's a working fiddle with all the various variables & events defined.

I've deliberately only populated two fields in this example simplify testing. I hoped to get a single JSON object that I can (eventually) hand off to AJAX, but I'm missing something. I also suspect that JSONObject.NULL may not be a Javascript construct, as I can actually only find references for it in Android & Java documentation, but it still seems like I should be able to use null in it's place?

So, my question is, how can I get null values into a JSON object as placeholders for text/ints/booleans that may or may not get populated by the user?

Edit to show functional code (see the full example at the fiddle link above):

To declare my JS object:

var pcbClientEvent = {
    eventType: {
        common: {
            user: null,
            pcbid: null,
            notes: null
        },
        assign: {
            sbePN: null,
            sbeJobNumber: null,
            sbeSerialNumber: null,
            sbeModelNumber: null
        }
    }
};

And to manipulate it after a form has been filled out (see the full example at the fiddle link above):

pcbClientEvent.eventType.common.user = $("#input_common_user").val() !== undefined ? $("#input_common_user").val() : pcbClientEvent.eventType.common.user;
pcbClientEvent.eventType.common.pcbid = $("#input_common_pcbid").val() !== undefined ? $("#input_common_pcbid").val() : pcbClientEvent.eventType.common.pcbid;
pcbClientEvent.eventType.common.notes = $("#input_common_notes").val() !== undefined ? $("#input_common_notes").val() : pcbClientEvent.eventType.common.notes;
pcbClientEvent.eventType.assign.sbePN = $("#input_assign_sbePN").val() !== undefined ? $("#input_assign_sbePN").val() : pcbClientEvent.eventType.assign.sbePN;
pcbClientEvent.eventType.assign.sbeJobNumber = $("#input_assign_sbeJobNumber").val() !== undefined ? $("#input_assign_sbeJobNumber").val() : pcbClientEvent.eventType.assign.sbeJobNumber;
pcbClientEvent.eventType.assign.sbeSerialNumber = $("#input_assign_sbeSerialNumber").val() !== undefined ? $("#input_assign_sbeSerialNumber").val() : pcbClientEvent.eventType.assign.sbeSerialNumber;
pcbClientEvent.eventType.assign.sbeModelNumber = $("#input_assign_sbeModelNumber").val() !== undefined ? $("#input_assign_sbeModelNumber").val() : pcbClientEvent.eventType.assign.sbeModelNumber;

Upvotes: 0

Views: 924

Answers (1)

Joel Allison
Joel Allison

Reputation: 2111

JSON is a lightweight data-interchange format. It can be helpful to think of the application's data model in terms of JSON, but it is not meant to be something that is directly manipulated in code. It is more natural to declare and modify a runtime object, serialize it to JSON for transport and parse it to instantiate the equivalent runtime object on the receiving side. JSON.net is just one of many excellent JSON libraries for the ASP.NET back-end of the application.

Specifically with respect to null values, these are directly supported in JSON. For example, consider this Javascript object:

var myObject = {
    a: "a string",
    b: 73,
    n : null
};

The result of performing JSON.stringify on myObject is:

 { "a": "a string", "b": 73, "n": null }

The approach should be to manipulate your runtime objects and generate JSON, rather than assembling fragments of JSON text.

Try reworking your example to build up a representative runtime object in Javascript and set your values directly, null values included. If there is a node in your model that should be represented in JSON but entirely empty (all null values), simply make sure to assign a suitable placeholder object in that location of the model. Then use JSON.stringify to see if the results meet your expectations.

Upvotes: 1

Related Questions