How to iterate through JSON? Or otherwise check if value exists in JSON? (solved)

I'm a bit stumped trying to determine if a given String is contained in my json.

Here's the stripped down starting point:

#include <ArduinoJson.h>
DynamicJsonDocument doc(1000); 
void setup() {
  Serial.begin(115200); 
}

void loop() {
  deserializeJson(doc, "[{\"name\":\"Marty\",\"uid\":\"asdf\"},{\"name\":\"Jim\",\"uid\":\"1234\"}]");
  String uid="asdf";

  // how to check if "asdf" is one of the UIDs inside the Json doc?

  delay(5000);
}

I'm trying to determine whether "asdf" is one of the uids contained in the json doc. If I could simply iterate through the json I'd be all set but so far no joy.

Here's the above json formatted:

[
   {
      "name":"Marty",
      "uid":"asdf"
   },
   {
      "name":"Jim",
      "uid":"1234"
   }
]

I don't imagine anyone has any ideas?

Well that took an embarassingly long time. For anyone else who comes this way:

#include <ArduinoJson.h>
DynamicJsonDocument doc(1000); // remember to allow enough size!



void setup() {
  Serial.begin(115200); 
}

void loop() {

  String payload = "[{\"name\":\"Marty\",\"uid\":\"asdf\"},{\"name\":\"Jim\",\"uid\":\"1234\"}]";
  deserializeJson(doc, payload);

  for (JsonObject elem : doc.as<JsonArray>()) {
    const char* name = elem["name"]; 
    const char* uid = elem["uid"]; 
    Serial.println(name);

    // or the dreaded String versions
    String name2 = elem["name"];
    Serial.println(name2);
  }  

  delay(5000);
}

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