Description

copyArray() copies values between a JsonArray (or JsonDocument) and a regular C array.

It works in both directions:

It supports both 1D and 2D arrays.

Support for JsonDocument was added in ArduinoJson 6.15.2

Signatures

// In the following lines T can be any type supported by JsonVariant
// (for example: bool, int, float, or const char*).
// N, N1, and N2 are integer constants.

// 1D array -> JsonArray/JsonDocument
bool copyArray(T[N] src, JsonArray dst);
bool copyArray(T[N] src, JsonDocument& dst);

// 1D array -> JsonArray/JsonDocument
bool copyArray(T* src, size_t len, JsonArray dst);
bool copyArray(T* src, size_t len, JsonDocument& dst);

// 2D array -> JsonArray/JsonDocument
bool copyArray(T[N1][N2] src, JsonArray dst);
bool copyArray(T[N1][N2] src, JsonDocument& dst);

// JsonArray/JsonDocument -> 1D array
size_t copyArray(JsonArrayConst src, T[N] dst);
size_t copyArray(const JsonDocument& src, T[N] dst);

// JsonArray/JsonDocument -> 1D array
size_t copyArray(JsonArrayConst src, T* dst, size_t len);
size_t copyArray(const JsonDocument& src, T* dst, size_t len);

// JsonArray -> 2D array
void copyArray(JsonArrayConst src, T[N1][N2] dst);
void copyArray(const JsonDocument& src, T[N1][N2] dst);

// these function also support JsonDocument

Return value

Some overloads of copyArray() return a bool that tells whether the copy was successful.

Some overloads of copyArray() return a size_t that contains the number of elements copied.

Example

int values[] = {1,2,3};

StaticJsonDocument<256> doc;
copyArray(values, doc.to<JsonArray>());
serializeJson(doc, Serial);

will write the following string to the serial port:

[1,2,3]
 X online