Search code examples
c++jsonjsoncpp

Assigning [null] in Json::Value in JsonCpp


I have an application which expects config in following JSON format but minified:

<config-json>
{
    "config" : {
        "services" : {
            "analytics" : {
                "sensor" : [
                {
                    "name" : "ip-sensor",
                    "server-name" : ["ip-server1"],
                    "export-name" : "ip-export1",
                    "resource" : "/ipv4",
                    "bulk" : [null] // <-- Notice 
                }
                ]
            }
        }
    }
}
</config-json>

In the above JSON config, the app expects "bulk" to be as [null] always. And that's a right expectation from app's perspective.

In my config generator code, I'm using JsonCpp to build the JSON objects by using Json::Value.

Because "bulk" needs to be [null], I'm populating it as follows:

//Json::Value *json_obj //Getting passed as an arg
(*json_obj)["config"]["services"]["analytics"]["sensor"][0]["bulk"] = Json::nullValue;

But what I get is:

"bulk":null // Notice the missing [] around null.

And hence the config gets discarded.

Is there a way in JsonCpp to achieve following:

"bulk" : [null]

Solution

  • Since brackets in Json denote an array, what you're going for is an array containing null. you should be able to do that like so:

    Json::Value jsonArray;
    jsonArray.append(Json::Value::null);
    (*json_obj)["config"]["services"]["analytics"]["sensor"][0][‌​"bulk"] = jsonArray;