JsonFilterExample.ino 2.5 KB

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