JsonFilterExample.ino 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  3. // MIT License
  4. //
  5. // This example shows how to use DeserializationOption::Filter
  6. //
  7. // https://arduinojson.org/v6/example/filter/
  8. #include <ArduinoJson.h>
  9. void setup() {
  10. // Initialize serial port
  11. Serial.begin(9600);
  12. while (!Serial) continue;
  13. // The huge input: an extract from OpenWeatherMap response
  14. const __FlashStringHelper* input_json = F(
  15. "{\"cod\":\"200\",\"message\":0,\"list\":[{\"dt\":1581498000,\"main\":{"
  16. "\"temp\":3.23,\"feels_like\":-3.63,\"temp_min\":3.23,\"temp_max\":4.62,"
  17. "\"pressure\":1014,\"sea_level\":1014,\"grnd_level\":1010,\"humidity\":"
  18. "58,\"temp_kf\":-1.39},\"weather\":[{\"id\":800,\"main\":\"Clear\","
  19. "\"description\":\"clear "
  20. "sky\",\"icon\":\"01d\"}],\"clouds\":{\"all\":0},\"wind\":{\"speed\":6."
  21. "19,\"deg\":266},\"sys\":{\"pod\":\"d\"},\"dt_txt\":\"2020-02-12 "
  22. "09:00:00\"},{\"dt\":1581508800,\"main\":{\"temp\":6.09,\"feels_like\":-"
  23. "1.07,\"temp_min\":6.09,\"temp_max\":7.13,\"pressure\":1015,\"sea_"
  24. "level\":1015,\"grnd_level\":1011,\"humidity\":48,\"temp_kf\":-1.04},"
  25. "\"weather\":[{\"id\":800,\"main\":\"Clear\",\"description\":\"clear "
  26. "sky\",\"icon\":\"01d\"}],\"clouds\":{\"all\":9},\"wind\":{\"speed\":6."
  27. "64,\"deg\":268},\"sys\":{\"pod\":\"d\"},\"dt_txt\":\"2020-02-12 "
  28. "12:00:00\"}],\"city\":{\"id\":2643743,\"name\":\"London\",\"coord\":{"
  29. "\"lat\":51.5085,\"lon\":-0.1257},\"country\":\"GB\",\"population\":"
  30. "1000000,\"timezone\":0,\"sunrise\":1581492085,\"sunset\":1581527294}}");
  31. // The filter: it contains "true" for each value we want to keep
  32. StaticJsonDocument<200> filter;
  33. filter["list"][0]["dt"] = true;
  34. filter["list"][0]["main"]["temp"] = true;
  35. // Deserialize the document
  36. StaticJsonDocument<400> doc;
  37. deserializeJson(doc, input_json, DeserializationOption::Filter(filter));
  38. // Print the result
  39. serializeJsonPretty(doc, Serial);
  40. }
  41. void loop() {
  42. // not used in this example
  43. }
  44. // See also
  45. // --------
  46. //
  47. // https://arduinojson.org/ contains the documentation for all the functions
  48. // used above. It also includes an FAQ that will help you solve any
  49. // deserialization problem.
  50. //
  51. // The book "Mastering ArduinoJson" contains a tutorial on deserialization.
  52. // It begins with a simple example, like the one above, and then adds more
  53. // features like deserializing directly from a file or an HTTP request.
  54. // Learn more at https://arduinojson.org/book/
  55. // Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤