Go Down

Topic: Parsing JSON with ArduinoJson (Read 85 times) previous topic - next topic

JamesGist

I have some json that I can't quite figure out how to parse. Here is the json.
Code: [Select]
{
  "columns": {
    "name": [
      "task 1",
      "task 2",
      "task 3",
      "task 4",
      "task 5"
    ]
  }
}


Here is what the ArduinoJson Assistant provides
Code: [Select]
onst size_t capacity = JSON_ARRAY_SIZE(5) + 2*JSON_OBJECT_SIZE(1) + 60;
DynamicJsonDocument doc(capacity);

const char* json = "{\"columns\":{\"name\":[\"task 1\",\"task 2\",\"task 3\",\"task 4\",\"task 5\"]}}";

deserializeJson(doc, json);

JsonArray columns_name = doc["columns"]["name"];
const char* columns_name_0 = columns_name[0]; // "task 1"
const char* columns_name_1 = columns_name[1]; // "task 2"
const char* columns_name_2 = columns_name[2]; // "task 3"
const char* columns_name_3 = columns_name[3]; // "task 4"


The main issue is that I don't know what name is going to be. so I need to capture that in a variable.
So far I have
Code: [Select]


void myHandler(const char *event, const char *data) {
 
  DynamicJsonDocument doc(capacity);
  deserializeJson(doc, data);

  JsonArray column = doc["columns"];
 
 ?? nameVariable =?? //Here is my struggle

//I need to be able to call...

JsonArray columns_name = doc["columns"][nameVariable];
}

arduarn

Where will the value of nameVariable come from?

JamesGist

Where will the value of nameVariable come from?
That is kind of the crux of my problem. If you look at the Json above, there is a "name" right under "columns".

I don't know how to index into the Json to populate the nameVariable

wildbill

Can you give an example of what your JSON actually looks like? I'm still not clear whether the literal 'name' appears in it.

arduarn

That is kind of the crux of my problem. If you look at the Json above, there is a "name" right under "columns".

I don't know how to index into the Json to populate the nameVariable
OK, then I think you want to use a JsonObject iterator.

Go Up