ESPAsyncWebServer JSON Response Example For ArduinoJson 6

TechOverflow LogoTechOverflow Consulting Blog Tools

The ESPAsyncWebserver page features an example for generating a basic JSON response using ArduinoJson:

example-2.cpp Copy Download#include "ArduinoJson.h"AsyncResponseStream *response = request->beginResponseStream("application/json"); DynamicJsonBuffer jsonBuffer; JsonObject &root = jsonBuffer.createObject(); root["heap"] = ESP.getFreeHeap(); root["ssid"] = WiFi.SSID(); root.printTo(*response); request->send(response);#include "ArduinoJson.h" AsyncResponseStream *response = request->beginResponseStream("application/json"); DynamicJsonBuffer jsonBuffer; JsonObject &root = jsonBuffer.createObject(); root["heap"] = ESP.getFreeHeap(); root["ssid"] = WiFi.SSID(); root.printTo(*response); request->send(response);

However, that example is made for ArduinoJson 5.x whereas most users want to use the updated ArduinoJson 6.x.

ArduinoJson 6.x minimal ESPAsyncWebserver example:

example-1.cpp Copy DownloadDynamicJsonDocument json(1024); json["status"] = "ok"; json["ssid"] = WiFi.SSID(); json["ip"] = WiFi.localIP(); serializeJson(json, *response); request->send(response);DynamicJsonDocument json(1024); json["status"] = "ok"; json["ssid"] = WiFi.SSID(); json["ip"] = WiFi.localIP(); serializeJson(json, *response); request->send(response);Check out similar posts by category: Embedded, ESP8266/ESP32, PlatformIOIf this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow Buy me a coffee

Tag » Arduino Json 6 Example