How to merge JSON objects?
This page explains how to write a function that merges two JSON objects into one.
Before using this function, please remember that ArduinoJson is a serialization library. As such, it is designed to serialize and deserialize JSON documents, not to store the state of an application.
Do not store the state of your application in a JsonDocument
, or you will create a memory leak.
Shallow merge
Suppose you have two JSON objects:
{
"name": "Benoit"
}
and
{
"age": 38
}
And suppose that your goal is to merge the two objects into one:
{
"name": "Benoit",
"age": 38
}
Well, you can do that easily with the following function:
void merge(JsonObject dest, JsonObjectConst src)
{
for (JsonPairConst kvp : src)
{
dest[kvp.key()] = kvp.value();
}
}
See online demo on wandbox.org.
Deep merge
The merge()
function above only merges the top-level object.
It blindly overrides members of the destination object.
Now, let’s see how we can merge the members instead of overriding them.
For example, imagine you have this object:
{
"config": {
"wifi": {
"ssid": "thebatcave"
},
"light": {
"state": "off"
}
}
}
And suppose you want to merge with the following object:
{
"config": {
"wifi": {
"pass": "i'm*batman!"
}
}
}
With a deep merging function, this would give:
{
"config": {
"wifi": {
"ssid": "thebatcave",
"pass": "i'm*batman!"
},
"light": {
"state": "off"
}
}
}
Here an example implementation for this function:
void merge(JsonVariant dst, JsonVariantConst src)
{
if (src.is<JsonObjectConst>())
{
for (JsonPairConst kvp : src.as<JsonObjectConst>())
{
if (dst[kvp.key()])
merge(dst[kvp.key()], kvp.value());
else
dst[kvp.key()] = kvp.value();
}
}
else
{
dst.set(src);
}
}
See online demo on wandbox.org.
This function uses: