JSON decoding with seemingly simple input - FlightAware

This is very embarrassing, trying to use the official JSON library code to work out:

How Many Aircraft? (in this case 1, there is at least "hex" followed by values for each aircraft)
Lat, Long, Mach of Aircraft?

from JSON input from /data/aircraft.json...

"

{"now":1640087670,"messages":29479289,"aircraft":[{"hex":"89619a","version":0,"sil_type":"unknown","mlat":[],"tisb":[],"messages":6,"seen":141.1,"rssi":-25.3},{"hex":"7c75a6","flight":"RSCU533 ","alt_baro":5000,"alt_geom":5375,"gs":133.5,"ias":127,"tas":140,"mach":0.212,"track":175.3,"track_rate":-0.06,"roll":0,"mag_heading":159.3,"baro_rate":0,"geom_rate":-32,"squawk":"4725","emergency":"none","category":"A7","nav_qnh":1013.6,"nav_altitude_mcp":5024,"nav_altitude_fms":5008,"nav_modes":["tcas"],"lat":-26.513947,"lon":152.937409,"nic":8,"rc":186,"seen_pos":0,"version":2,"nic_baro":1,"nac_p":9,"nac_v":2,"sil":3,"sil_type":"perhour","gva":2,"sda":2,"mlat":[],"tisb":[],"messages":925,"seen":0,"rssi":-3.8}]}

"

but I can't get any Keys()?!?

Making a radar display, JSON parsing is nothing new to me in C++, but just the native library is driving me nuts...?

Please help :slight_smile:

everybody uses ArduinoJson library by Benoit Blanchon

Which is the official library you mean?

Instead of Arduinbo_JSON, try the library from ArduinoJson.org. They have an assistant that will write the Serialize or Deserialize code for you:

Note: Your JSON has some empty fields ("mlat" and "tisb") which the Assistant doesn't like.

{
  "now": 1640087670,
  "messages": 29479289,
  "aircraft": [
    {
      "hex": "89619a",
      "version": 0,
      "sil_type": "unknown",
      "mlat": "",
      "tisb": "",
      "messages": 6,
      "seen": 141.1,
      "rssi": -25.3
    },
    {
      "hex": "7c75a6",
      "flight": "RSCU533 ",
      "alt_baro": 5000,
      "alt_geom": 5375,
      "gs": 133.5,
      "ias": 127,
      "tas": 140,
      "mach": 0.212,
      "track": 175.3,
      "track_rate": -0.06,
      "roll": 0,
      "mag_heading": 159.3,
      "baro_rate": 0,
      "geom_rate": -32,
      "squawk": "4725",
      "emergency": "none",
      "category": "A7",
      "nav_qnh": 1013.6,
      "nav_altitude_mcp": 5024,
      "nav_altitude_fms": 5008,
      "nav_modes": [
        "tcas"
      ],
      "lat": -26.513947,
      "lon": 152.937409,
      "nic": 8,
      "rc": 186,
      "seen_pos": 0,
      "version": 2,
      "nic_baro": 1,
      "nac_p": 9,
      "nac_v": 2,
      "sil": 3,
      "sil_type": "perhour",
      "gva": 2,
      "sda": 2,
      "mlat": "",
      "tisb": "",
      "messages": 925,
      "seen": 0,
      "rssi": -3.8
    }
  ]
}

Hi @hippyau
To verify that a json string is correct, use the website
online json viewer:

In your case informs that your json is not valid.

This is awesome.... but how can I work out how many aircaraft there are?

JsonObject aircraft_0 = doc["aircraft"][0];

I would guess:

JsonArray aircraft = doc["aircraft"].as<JsonArray>();
size_t numberOfAircraft = aircraft.size();

If you just want to process all of the entries you don't need the count first:

for(JsonObject a : aircraft) {
    Serial.println(a["hex"].as<const char *>());
    Serial.println(a["flight"].as<const char *>());
    Serial.println(a["alt_baro"].as<int>());
}
1 Like

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