| 1 | // ArduinoJson - https://arduinojson.org |
| 2 | // Copyright © 2014-2026, Benoit BLANCHON |
| 3 | // MIT License |
| 4 | // |
| 5 | // This example shows how to generate a JSON document with ArduinoJson. |
| 6 | // |
| 7 | // https://arduinojson.org/v7/example/generator/ |
| 8 | |
| 9 | #include <ArduinoJson.h> |
| 10 | |
| 11 | void setup() { |
| 12 | // Initialize Serial port |
| 13 | Serial.begin(9600); |
| 14 | while (!Serial) |
| 15 | continue; |
| 16 | |
| 17 | // Allocate the JSON document |
| 18 | JsonDocument doc; |
| 19 | |
| 20 | // Add values in the document |
| 21 | doc["sensor"] = "gps"; |
| 22 | doc["time"] = 1351824120; |
| 23 | |
| 24 | // Add an array |
| 25 | JsonArray data = doc["data"].to<JsonArray>(); |
| 26 | data.add(48.756080); |
| 27 | data.add(2.302038); |
| 28 | |
| 29 | // Generate the minified JSON and send it to the Serial port |
| 30 | serializeJson(doc, Serial); |
| 31 | // The above line prints: |
| 32 | // {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]} |
| 33 | |
| 34 | // Start a new line |
| 35 | Serial.println(); |
| 36 | |
| 37 | // Generate the prettified JSON and send it to the Serial port |
| 38 | serializeJsonPretty(doc, Serial); |
| 39 | // The above line prints: |
| 40 | // { |
| 41 | // "sensor": "gps", |
| 42 | // "time": 1351824120, |
| 43 | // "data": [ |
| 44 | // 48.756080, |
| 45 | // 2.302038 |
| 46 | // ] |
| 47 | // } |
| 48 | } |
| 49 | |
| 50 | void loop() { |
| 51 | // not used in this example |
| 52 | } |
| 53 | |
| 54 | // See also |
| 55 | // -------- |
| 56 | // |
| 57 | // https://arduinojson.org/ contains the documentation for all the functions |
| 58 | // used above. It also includes an FAQ that will help you solve any |
| 59 | // serialization problem. |
| 60 | // |
| 61 | // The book "Mastering ArduinoJson" contains a tutorial on serialization. |
| 62 | // It begins with a simple example, like the one above, and then adds more |
| 63 | // features like serializing directly to a file or an HTTP request. |
| 64 | // Learn more at https://arduinojson.org/book/ |
| 65 | // Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤ |