Array multidimensional to JSON

I have two dimensional array from sensor reading, id and time_update.
for an example i write loop to adding value to array like this :

String biDimArray[5][2];

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println();
  int a, b;
  for (a = 0; a < 5; a++) {    //edited
    biDimArray[a][0] = String(analogRead(A0));
    biDimArray[a][1] = "2023-07-03 15:17:01";
    delay(15);
  }
  Serial.println();
  Serial.println("My Two Dimensional Array:");
  for (a = 0; a < 35; a++) {
    for (b = 0; b < 2; b++) {
      Serial.print(biDimArray[a][b]);
      Serial.print(' ');
    }
    Serial.println();
  }
  Serial.println();
}

void loop() {
  // put your main code here, to run repeatedly:

}

{
{"13", "2023-07-03 15:17:01"},
{"77", "2023-07-03 15:17:01"},
{"8", "2023-07-03 15:17:01"},
{"113", "2023-07-03 15:17:01"},
{"17", "2023-07-03 15:17:01"}
}

now i want encode this array to JSON. I already using Assistant | ArduinoJson 6 but i need to create dynamic function like

void sendJson(array){
  somevar = encodeJson(array);
  httpCode = httpPost(somevar);
}

You declare your 2D array
String biDimArray[5][2];

and then index into it from 0...34????

  for (a = 0; a < 35; a++) {
    biDimArray[a][0] = String(analogRead(A0));
    biDimArray[a][1] = "2023-07-03 15:17:01";
    delay(15);
  }

You have gone WAY beyond then end of your array and wandered off into unknown territory.

i'm so sorry, my bad..
My looping for just generate random, and already tested. I forgot to edit iteration to 5. Because 35 array too much data for showing in this post.

you JSON will have to look something like this:

{
 "0": ["13", "2023-07-03 15:17:01"],
 "1": ["77", "2023-07-03 15:17:01"],
"2": ["8", "2023-07-03 15:17:01"],
"3": ["113", "2023-07-03 15:17:01"],
"4": ["17", "2023-07-03 15:17:01"]
}

where your name field is the index of your array and the data is the actual 2 element array

I've got code from this forum

#include <ArduinoJson.h>

char responseBody[] = R"raw(
{
  "actions": [
    {
      "action":"updateProperty",
      "property":"some.property.1",
      "value":"some value 1"
    },
    {
      "action":"updateProperty",
      "property":"some.property.2",
      "value":"some value 2"
    },
    {
      "action":"updateProperty",
      "property":"some.property.3",
      "value":"some value 3"
    }
  ]
})raw";

void setup(){
  Serial.begin(115200);
  delay(200);
  Serial.println();
  Serial.println();
  int length = strlen( responseBody); // sizeof(responseBody);
  Serial.print( F("JSON response length: ") );  // always one char longer than the string?
  Serial.println(length);
  StaticJsonBuffer<500> jsonBuffer;
  JsonObject& json = jsonBuffer.parseObject(responseBody);
  char JSONmessageBuffer[500];
  json.prettyPrintTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));
  if (!json.success()) {
    Serial.println(F("Failed to parse responseBody"));
  } else {
    Serial.println(JSONmessageBuffer);
  }
}

void loop()
{}

when i change char responseBody[] to my String array[][]

  int a, b;
  String biDimArray[5][2];
  int al = analogRead(A0);
  for (a = 0; a < 5; a++) {
    biDimArray[a][0] = al;
    biDimArray[a][1] = "2023-07-05 11:20:33";
    delay(15);
  }

got error

85 |   typedef typename StringTraits<TChar *>::Reader TReader;
87 |   typedef JsonParser<TReader, TWriter> TParser;
78 |     return Internals::makeParser(that(), json, nestingLimit).parseObject();
96 | inline typename JsonParserBuilder<TJsonBuffer, TString>::TParser makeParser(
......

go back some steps.
Why a two dimensional array at all?

It seems you have two different components.
a value
a timestamp

So why not use a structure?
why are String Objects needed?

I propose to start with something like this

struct Data {
   int value;   // or what ever type is really needed...
   char timestamp[20];
};

// you can create an array with structure Data
Data data[5];

I have bad skill about this.. :sweat:
Can you give me example how to add value to that struct, access and parsing to json..

input
processing
output

of a structure

what's the usage of the JSON?
on which microcontroller?
Is there any need of another JSON in the sketch?
if not - I would just generate that JSON manually ...
How should the JSON look like? Can you give a valid example or a specification?

1 Like

It works..

I'm so sorry maybe my question lil bit confusing, i just wanna know how to encode dynamic array to json, and that json i wanna send it to server.

I'm using your code like this :

struct Data{
  int id;
  char timestamp[20];
};

Data data[5];

void setup(){
  Serial.begin(115200);
  int val = analogRead(A0);
  for (int i = 0; i < 5; i++){
    data[i] = (Data) {val, "2023-07-05 11:20:29"};
  }
  int sizeAr = sizeof(data) / sizeof(data[0]);
  Serial.println();
  Serial.println();
  for(int a = 0; a < sizeAr; a++){
    Serial.print(a);
    Serial.print("\t");
    Serial.print(data[a].id);
    Serial.print("\t");
    Serial.print(data[a].timestamp);
    Serial.println();
  }
}

void loop()
{}

and serial show like this :

0	3	2023-07-05 11:20:29
1	3	2023-07-05 11:20:29
2	3	2023-07-05 11:20:29
3	3	2023-07-05 11:20:29
4	3	2023-07-05 11:20:29

Actually i already got the answer how to convert dynamic array (not using struct) to json using arduinoJson

#include <ArduinoJson.h>

String biDimArray[15][2];

void setup(){
  Serial.begin(115200);
  int a, b;
  int al = analogRead(A0);
  for (a = 0; a < 15; a++) {
    biDimArray[a][0] = al + a;
    biDimArray[a][1] = "2023-07-05 11:20:33";
    delay(15);
  }
  delay(200);
  Serial.println();
  Serial.println();
  StaticJsonDocument<500> jsonBuffer;
  copyArray(biDimArray, jsonBuffer);
  String json;  
  serializeJsonPretty(jsonBuffer, json);
  Serial.println(json);
}

void loop()
{}
[
  [
    "3",
    "2023-07-05 11:20:33"
  ],
  [
    "4",
    "2023-07-05 11:20:33"
  ],
  [
    "5",
    "2023-07-05 11:20:33"
  ],
  [
    "6",
    "2023-07-05 11:20:33"
  ],
.........

But i think using struct must be better json format, and when i try using same code (array to json)

#include <ArduinoJson.h>

struct Data{
  int id;
  char timestamp[20];
};

Data data[5];

void setup(){
  Serial.begin(115200);
  int val = analogRead(A0);
  for (int i = 0; i < 5; i++){
    data[i] = (Data) {val, "2023-07-05 11:20:29"};
  }  
  delay(200);
  Serial.println();
  Serial.println();
  StaticJsonDocument<500> jsonBuffer;
  copyArray(data, jsonBuffer);
  String json;  
  serializeJsonPretty(jsonBuffer, json);
  Serial.println(json);
}

void loop()
{}

But showing error

17 |     convertToJson(src, dst); // Error here? See https://arduinojson.org/v6/unsupported-set/
..........

Maybe you can help me why this error happen.. :smiling_face_with_tear:

I asked you serveral questions because I think the answers are ESSENTIAL to give you a better example:

if you would just check the link you gave in your starting post

and see the JSON from that example you would recognize, that usually a JSON array consists of pairs of "tag" and "value" ... and up to now you haven't described how these tags (for your values) should be named and how the node name should be called.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.