search for object in json before adding a new one

Hello ,
I have a working code that scan my wifi
when he find a device he add it to the json , then send it to my server.

I have notice that I get the same data (meaning the same device ) on one scan
I want to avoid it ,
for example :
this is the json it build me

POSTed: {"probes":[{"address":"dc:d6:3f:37:ba:2c","rssi":-59},{"address":"dc:d6:3f:37:ba:2c","rssi":-49}]}

I don't need it to put the same device twice
I want to see if the mac is excite
if yes - don't add the new scan resualt
if not - add the new scan resualt

this is the part of the code that create the JSON

  String json = "";
  DynamicJsonBuffer  jsonBuffer;
  JsonObject& root = jsonBuffer.createObject();
  JsonArray& probes = root.createNestedArray("probes");
  for (WiFiEventSoftAPModeProbeRequestReceived w : myList) {
    JsonObject& probe = probes.createNestedObject();
    probe["address"] = macToString(w.mac);
    probe["rssi"] = w.rssi;
   }
  myList.clear();
  root.printTo(json);
  Serial.println("json:" + json);

is there a command for "search if xxx in json"?

Thanks,

Whithout having any experience with ArduinoJson, isn't it a simple matter of:

bool hasMacAddress(JsonArray &probes, String &mac)
{
  for (int i = 0; i < probes.size(); i++) if (mac.equals(probes[i]["address"])) return true;
  return false;
}

I hope the code is not supposed to run on an AVR based board..

but on the first devices the probes.size() will be 0 (there is nothing there)
so it will do some problems , no ?

and why

I hope the code is not supposed to run on an AVR based board..

?

I'm using node mcu-esp-12e

The data type of "myList" is missing in your snippet, the sollution could be to find duplicates in that list if the json approach does not work.

sorry ,
this is the full code

#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>
#include <vector>

const char* apSsid     = "ap-ssid";
const char* apPassword = "ap-password";
cconst char* clientSsid     = "David";
const char* clientPassword = "12345";

HTTPClient http;

WiFiEventHandler probeRequestPrintHandler;

String macToString(const unsigned char* mac) {
  char buf[20];
  snprintf(buf, sizeof(buf), "%02x:%02x:%02x:%02x:%02x:%02x",
           mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  return String(buf);
}

std::vector<WiFiEventSoftAPModeProbeRequestReceived> myList;

void onProbeRequestPrint(const WiFiEventSoftAPModeProbeRequestReceived& evt) {
  myList.push_back(evt);
}

void setup() {
  Serial.begin(115200);
  Serial.println("Hello!");

  // Don't save WiFi configuration in flash - optional
  WiFi.persistent(false);

  WiFi.mode(WIFI_AP_STA);
  WiFi.softAP(apSsid, apPassword);
  WiFi.begin(clientSsid, clientPassword);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(100);
  }
  Serial.println("");
  probeRequestPrintHandler = WiFi.onSoftAPModeProbeRequestReceived(&onProbeRequestPrint);
}

void loop() {
  delay(10000);
  String json = "";
  DynamicJsonBuffer  jsonBuffer;
  JsonObject& root = jsonBuffer.createObject();
  JsonArray& probes = root.createNestedArray("probes");
  for (WiFiEventSoftAPModeProbeRequestReceived w : myList) {
    JsonObject& probe = probes.createNestedObject();
    probe["address"] = macToString(w.mac);
    probe["rssi"] = w.rssi;
    //  Serial.print("MAC :___ ");
    //  Serial.println(macToString(w.mac));
    //  Serial.print("RSSI : ___ ");
    //  Serial.println(w.rssi);
  }
  myList.clear();
  root.printTo(json);
  Serial.println("json:" + json);

  http.begin("http://10.0.0.188/");
  http.addHeader("Content-Type", "application/json");
  http.POST(json);
  http.end();
}

There are some problems with the posted code. First of all, this does not look right:

void onProbeRequestPrint(const WiFiEventSoftAPModeProbeRequestReceived& evt) {
  myList.push_back(evt);
}

The values in "evt" may change after the handler returns if the "evt" variable is re-used internally by "probeRequestPrintHandler" and this would affect "myList".

I suppose that wifi is based on interrupts, and that may cause another issue because "onProbeRequestPrint" may be called while you are accessing the contents of "myList" in "loop". You need to make sure that only one part of the code may access "myList" at a time.

The duplicate issue may be solved by checking if the MAC is already present in "myList" before adding it in "probeRequestPrintHandler".

this is not my code , this is what I have found while searching WiFI_Device_Finder and it's seem to be working .
it's seem that only 1 part is access "myList"

(if you know a better code\app that scan and "see" the WiFi devices around me (not AP) it will great! )

and about hte duplicate
if I create a new list "Final_List"
and put there every "item" that in apper only once from myList?
then change the

 for (WiFiEventSoftAPModeProbeRequestReceived w : myList) {

to

 for (WiFiEventSoftAPModeProbeRequestReceived w :Final_List) {

will this help?

Thanks,