0

I am using ArduinoJson and ESP8266HTTPClient.

Say I have a DynamicJsonDocument that has an unknown number of entries with information on different persons:

[
    {
        "ID": "12345", 
        "name": "John Smith", 
        "Address": "Street 1"
    }, 

    .
    .
    .

    {
        "ID": "00345", 
        "name": "Paul Anderson", 
        "Address": "Street 2"
    }
]

I would like to somehow store the values associated with the key "name" for all the different persons. For example, have something like:

String name1 = "";
...
String nameN = "";

.
.
.

Serial.println(name1);
...
Serial.println(nameN);

To print a list of all the names:

John Smith
Paul Anderson

The only information of the JSON I have beforehand is the keys each entry will have. I do not know how many entries there will be.

How can I go about this?

EDIT: Why I want to do this:

I am using the ESP as a client to get information from a server in the form of a JSON similar to what I wrote above. At the same time, I am serving a simple HTML website by doing server.send(200, "text/html", HTML_code); using the ESP as an AP. I was able to just cram the whole JSON into the HTML as text, but I would like to just have specific information from the JSON, as explained above.

I am aware what I am doing is far from elegant or the "correct way to do it", but I am learning all of this, so any criticism and help is more than welcome :)

5
  • 1
    You may want to make it clear why you want to do this. Because, as is, it sounds like you're just duplicating data for no reason. And if there is a good reason for doing this, it will likely give some shape to the answer.
    – timemage
    Feb 2 at 13:33
  • 1
    You know the size from JsonArray::size() so just iterate over the array getting each object in turn. It's also recommended to use iterators instead of a for loop.
    – Majenko
    Feb 2 at 13:49
  • I added an edit explaining just that, maybe now it is more clear?
    – choreley
    Feb 2 at 14:48
  • your explanation seems to suggest that you are thinking that the webpage HTML needs to exist in its entirety before the webpage can be served ... you can serve the webpage in pieces ... serve the <html><header>...</header><body> then iterate the JSON <div> put name here </div>, then serve </body></html>
    – jsotola
    Feb 2 at 18:01
  • for simple extractions like that, in the past I've just plucked the values from the JSON as text, using something like strstr(json, "\"key\"") and then strchr(thatString, ',') to get the span from that key's starting point. You could use String methods too, but those tend to cause problems if over-used.
    – dandavis
    Feb 2 at 18:12
1

Directly from the ArduinoJson Assistant:

// Stream& input;

StaticJsonDocument<384> doc;
deserializeJson(doc, input);

for (JsonObject elem : doc.as<JsonArray>()) {
  const char* ID = elem["ID"]; // "12345", "13244", "00345"
  const char* name = elem["name"]; // "John Smith", "Jason Statham", "Paul Anderson"
  const char* Address = elem["Address"]; // "Street 1", "Street 2", "Street 3"
}

See also:

3
  • Thank you, I will try this. How would I access for example "Jason Statham" inside the "name" char*?
    – choreley
    Feb 2 at 15:16
  • Serial.println(name); // prints "Jason Statham" Feb 2 at 16:01
  • Wouldn't that print all the names and not a specific one?
    – choreley
    Feb 2 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.