Damago
Damago

Reputation: 137

ArduinoJson - how to merge two arrays into one?

How can I merge two JSON documents into one?

JSONDocument doc1;
JSONDocument doc2;

doc1["test"]["a"]["b"]="1";
doc1["test"]["a"]["c"]="2";

doc2["test"]["a"]["d"]="3";
doc2["test"]["a"]["c"]="4";

merged doc should be something like a result of such assignments:

doc["test"]["a"]["b"]="1";
doc["test"]["a"]["d"]="3";
doc["test"]["a"]["c"]="4";

As you see I am tring to "add" two variables doc1 and doc2 to achieve a json which will have elements from both doc1 and doc2 together in 1 json document. I was trying something like doc["test"].add(doc1["test"]) etc.

The background is that I am using ArduinoJson to create JSON with sensors data. The problem is that I have 2 sets of sensors, each returns own results. I cannot make ArduinoJson to combine those results into one single JSON document.

The code is:

    doc["test"]["dht"]=DHT11.getValuesJson();      // this is result from first sensors
    doc["test"]["dallas"]=DallasSensors.getValuesJson(); // result from other sensors
    // my attempts to create joined array with results from both sensor sets:
    doc["test"]["temp1"]=DallasSensors.getValuesJson();
    doc["test"]["temp1"].add(DHT11.getValuesJson());
    doc["test"]["temp2"].set(DallasSensors.getValuesJson());
    doc["test"]["temp2"].add(DHT11.getValuesJson());

The resulting data is as below. As you see I have two correct jsons, one from dht-temperature, another from dallas sensors, but the combined json has only data from dallas:

    "test": {
        "dht": {
            "temp": {
                "dht-temperature": "N.A."
            },
            "humidity": {
                "dht-humidity": "N.A."
            }
        },
        "dallas": {
            "temp": {
                "28388FD60B00003F": "27.69",
                "28EE60292E1601AE": "27.75",
                "28EEFA4B2E160133": "27.75",
                "28EED65D2E1601DB": "28.00",
                "28EE215C2E1601A3": "27.44",
                "28EE77562E1601B1": "27.56"
            }
        },
        "temp1": {
            "temp": {
                "28388FD60B00003F": "27.69",
                "28EE60292E1601AE": "27.75",
                "28EEFA4B2E160133": "27.75",
                "28EED65D2E1601DB": "28.00",
                "28EE215C2E1601A3": "27.44",
                "28EE77562E1601B1": "27.56"
            }
        },
        "temp2": {
            "temp": {
                "28388FD60B00003F": "27.69",
                "28EE60292E1601AE": "27.75",
                "28EEFA4B2E160133": "27.75",
                "28EED65D2E1601DB": "28.00",
                "28EE215C2E1601A3": "27.44",
                "28EE77562E1601B1": "27.56"
            }
        }

as you see ArduinoJson is totally ignoring any attempts to append data from second json to the first one. Both .add() and .set() functions seem to do nothing.

Expected result:

    "data": {
        "temp": {
            "28388FD60B00003F": "27.69",
            "28EE60292E1601AE": "27.75",
            "28EEFA4B2E160133": "27.75",
            "28EED65D2E1601DB": "28.00",
            "28EE215C2E1601A3": "27.44",
            "28EE77562E1601B1": "27.56",
            "dht-temperature": "ERROR"
        },
        "humidity": {
            "dht-humidity": "N.A."
        },

Can anybody help me with this? I am totally lost. I have already wasted more time on trying to do this with this library than with creating json manually.

Upvotes: 0

Views: 152

Answers (1)

Damago
Damago

Reputation: 137

Ok. I found something like this, however I did not test it yet, there is a code discussing merging two JsonDocuments. It is using JsonVariant and not JsonDocument and I hope it will be possible to use it without large modifications for JsonDocument. It seems that the library ArduinoJSON itself is meant to be serialization/deserialization only library and there is no merge functionality.

void JSONmerge(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);
   }
}

To convert JsonDocument to JsonVariant it should be enough to do:

// create a variant
JsonVariant variant = doc.to<JsonVariant>();

Upvotes: 0

Related Questions