You are repeatedly overwriting the ssid
property of the same object.
Instead, you want to write different properties, like
wifiJsonObject["ssid" + String(i+1)] = WiFi.SSID(i);
Then, only serialize the document once, when it is complete.
That being said, I strongly concur with Juraj in that the correct way to
represent a list in JSON (and wifiList
is clearly meant to be a list)
is by using an array:
void scanWifiNetworks() {
DynamicJsonDocument jsonDoc(1024);
JsonArray wifiList = jsonDoc.createNestedArray("wifiList");
int n = WiFi.scanNetworks();
for (int i = 0; i < n; ++i) {
JsonObject wifiNet = wifiList.createNestedObject();
wifiNet["ssid"] = WiFi.SSID(i);
}
serializeJsonPretty(jsonDoc, SerialBT);
serializeJsonPretty(jsonDoc, Serial);
}
This should produce a JSON document that looks like this:
{
"wifiList": [
{ "ssid": "quark" },
{ "ssid": "Airtel_Zerotouch" },
{ "ssid": "NETGEAR77" },
{ "ssid": "Itobuz-technologies" },
{ "ssid": "Nokia 6.1" }
]
}
Edit: Note that, if it's clear from the context (e.g. a request URL) that you are transmitting a list of WiFi networks, then you can get rid of the outer object and the "wifiList" label, and use the array as the JSON document:
[
{ "ssid": "quark" },
{ "ssid": "Airtel_Zerotouch" },
{ "ssid": "NETGEAR77" },
{ "ssid": "Itobuz-technologies" },
{ "ssid": "Nokia 6.1" }
]
As suggested by Jaromanda X, if you are never going to extend the inner objects with extra information, you can further simplify the document by removing those objects and just listing the SSIDs in the array:
[
"quark",
"Airtel_Zerotouch",
"NETGEAR77",
"Itobuz-technologies",
"Nokia 6.1"
]