Convert Figma logo to code with AI

bblanchon logoArduinoJson

📟 JSON library for Arduino and embedded C++. Simple and efficient.

6,701
1,120
6,701
18

Top Related Projects

42,154

JSON for Modern C++

14,146

A fast JSON parser/generator for C++ with both SAX/DOM style API

8,063

A C++ library for interacting with JSON.

19,255

Parsing gigabytes of JSON per second : used by Facebook/Meta Velox, the Node.js runtime, ClickHouse, WatermelonDB, Apache Doris, Milvus, StarRocks

C/C++ JSON parser/generator benchmark

Quick Overview

ArduinoJson is a popular JSON library for Arduino and IoT projects. It provides a simple and efficient way to parse, serialize, and manipulate JSON data on embedded systems with limited resources. The library is designed to be both easy to use and memory-efficient, making it ideal for microcontroller-based projects.

Pros

  • Memory efficient, suitable for embedded systems with limited RAM
  • Easy to use API for parsing and generating JSON
  • Supports both dynamic and static memory allocation
  • Extensive documentation and examples available

Cons

  • May have a steeper learning curve for beginners compared to simpler JSON libraries
  • Some advanced features might be overkill for very simple projects
  • Performance can be impacted when dealing with very large JSON documents on resource-constrained devices

Code Examples

  1. Parsing JSON:
StaticJsonDocument<200> doc;
DeserializationError error = deserializeJson(doc, jsonString);

if (error) {
  Serial.println("Failed to parse JSON");
  return;
}

const char* sensor = doc["sensor"];
long time = doc["time"];
double latitude = doc["data"][0];
double longitude = doc["data"][1];
  1. Creating JSON:
StaticJsonDocument<200> doc;
doc["sensor"] = "gps";
doc["time"] = 1351824120;
JsonArray data = doc.createNestedArray("data");
data.add(48.756080);
data.add(2.302038);

String output;
serializeJson(doc, output);
  1. Working with arrays:
StaticJsonDocument<300> doc;
JsonArray array = doc.to<JsonArray>();

array.add("hello");
array.add(42);
array.add(3.14);

for (JsonVariant v : array) {
  Serial.println(v.as<String>());
}

Getting Started

  1. Install the ArduinoJson library:

    • In the Arduino IDE, go to Sketch > Include Library > Manage Libraries
    • Search for "ArduinoJson" and install the latest version
  2. Include the library in your sketch:

#include <ArduinoJson.h>

void setup() {
  Serial.begin(9600);

  StaticJsonDocument<200> doc;
  doc["message"] = "Hello, ArduinoJson!";
  
  String output;
  serializeJson(doc, output);
  Serial.println(output);
}

void loop() {
  // Your main code here
}
  1. Upload the sketch to your Arduino board and open the Serial Monitor to see the JSON output.

Competitor Comparisons

42,154

JSON for Modern C++

Pros of nlohmann/json

  • More feature-rich and versatile, supporting a wider range of JSON operations
  • Better performance for large JSON datasets
  • Extensive documentation and examples

Cons of nlohmann/json

  • Larger library size, which may be an issue for memory-constrained devices
  • Steeper learning curve due to more complex API
  • Not specifically optimized for embedded systems like Arduino

Code Comparison

ArduinoJson:

StaticJsonDocument<200> doc;
doc["sensor"] = "gps";
doc["time"] = 1351824120;
String output;
serializeJson(doc, output);

nlohmann/json:

json j;
j["sensor"] = "gps";
j["time"] = 1351824120;
std::string output = j.dump();

Both libraries offer similar basic functionality for creating and serializing JSON objects. However, nlohmann/json provides more advanced features and flexibility for complex JSON operations, while ArduinoJson is optimized for embedded systems with limited resources.

14,146

A fast JSON parser/generator for C++ with both SAX/DOM style API

Pros of rapidjson

  • Higher performance and lower memory usage
  • Support for more advanced JSON features (e.g., UTF-8/16/32, int64)
  • Better suited for large-scale applications and complex JSON structures

Cons of rapidjson

  • Steeper learning curve and more complex API
  • Less suitable for resource-constrained environments like Arduino
  • Requires C++11 or later, limiting compatibility with older systems

Code Comparison

ArduinoJson:

StaticJsonDocument<200> doc;
doc["sensor"] = "gps";
doc["time"] = 1351824120;
String output;
serializeJson(doc, output);

rapidjson:

rapidjson::Document doc;
doc.SetObject();
doc.AddMember("sensor", "gps", doc.GetAllocator());
doc.AddMember("time", 1351824120, doc.GetAllocator());
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
doc.Accept(writer);
std::string output = buffer.GetString();

ArduinoJson is designed for simplicity and ease of use, particularly in embedded systems. It offers a more straightforward API and better integration with Arduino-specific features. rapidjson, on the other hand, provides higher performance and more advanced features, making it more suitable for larger-scale applications and complex JSON processing tasks. The code comparison illustrates the difference in complexity between the two libraries, with ArduinoJson offering a more concise and intuitive approach.

8,063

A C++ library for interacting with JSON.

Pros of JsonCpp

  • More comprehensive and feature-rich for general C++ applications
  • Better support for complex JSON structures and advanced parsing
  • Wider platform compatibility beyond Arduino environments

Cons of JsonCpp

  • Larger memory footprint and higher resource usage
  • More complex API, potentially steeper learning curve
  • Not optimized for embedded systems or microcontrollers

Code Comparison

ArduinoJson:

StaticJsonDocument<200> doc;
deserializeJson(doc, jsonString);
String name = doc["name"];
int age = doc["age"];

JsonCpp:

Json::Value root;
Json::Reader reader;
reader.parse(jsonString, root);
std::string name = root["name"].asString();
int age = root["age"].asInt();

Both libraries offer JSON parsing capabilities, but ArduinoJson is more streamlined for embedded systems, while JsonCpp provides a more robust solution for general C++ applications. ArduinoJson's API is simpler and more memory-efficient, making it ideal for Arduino and other microcontroller projects. JsonCpp, on the other hand, offers more advanced features and better handles complex JSON structures, making it suitable for larger-scale applications on various platforms.

19,255

Parsing gigabytes of JSON per second : used by Facebook/Meta Velox, the Node.js runtime, ClickHouse, WatermelonDB, Apache Doris, Milvus, StarRocks

Pros of simdjson

  • Extremely fast JSON parsing, optimized for modern CPUs
  • Supports parsing gigabytes of JSON per second
  • Designed for high-performance applications and big data processing

Cons of simdjson

  • Not specifically designed for embedded systems or microcontrollers
  • Higher memory footprint compared to ArduinoJson
  • Steeper learning curve for beginners

Code Comparison

ArduinoJson:

StaticJsonDocument<200> doc;
deserializeJson(doc, jsonString);
const char* sensor = doc["sensor"];
long time = doc["time"];
double latitude = doc["data"][0];

simdjson:

ondemand::parser parser;
ondemand::document doc = parser.iterate(jsonString);
string_view sensor = doc["sensor"];
int64_t time = doc["time"];
double latitude = doc["data"].at(0);

Summary

ArduinoJson is tailored for Arduino and embedded systems, offering a user-friendly API and efficient memory usage. simdjson, on the other hand, focuses on high-performance JSON parsing for modern CPUs, making it ideal for big data applications. While simdjson excels in speed, ArduinoJson is more suitable for resource-constrained environments and provides an easier learning curve for beginners.

C/C++ JSON parser/generator benchmark

Pros of nativejson-benchmark

  • Focuses on benchmarking multiple JSON libraries, providing comprehensive performance comparisons
  • Supports a wide range of JSON libraries and parsing techniques
  • Offers detailed performance metrics and analysis tools

Cons of nativejson-benchmark

  • Not designed for direct use in Arduino projects, unlike ArduinoJson
  • Lacks the simplicity and ease of integration that ArduinoJson provides for embedded systems
  • May have a steeper learning curve for users primarily interested in JSON parsing for Arduino

Code Comparison

ArduinoJson:

StaticJsonDocument<200> doc;
deserializeJson(doc, jsonString);
String name = doc["name"];
int age = doc["age"];

nativejson-benchmark (using RapidJSON as an example):

Document d;
d.Parse(jsonString);
std::string name = d["name"].GetString();
int age = d["age"].GetInt();

While both libraries can parse JSON, ArduinoJson is specifically optimized for Arduino and embedded systems, offering a more straightforward API. nativejson-benchmark, on the other hand, focuses on benchmarking various JSON libraries and provides a platform for performance comparison rather than being a standalone JSON parsing solution.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

ArduinoJson


GitHub Workflow Status Continuous Integration Fuzzing Status Coveralls branch
GitHub stars GitHub Sponsors

ArduinoJson is a C++ JSON library for Arduino and IoT (Internet Of Things).

Features

Quickstart

Deserialization

Here is a program that parses a JSON document with ArduinoJson.

const char* json = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";

JsonDocument doc;
deserializeJson(doc, json);

const char* sensor = doc["sensor"];
long time          = doc["time"];
double latitude    = doc["data"][0];
double longitude   = doc["data"][1];

See the tutorial on arduinojson.org

Serialization

Here is a program that generates a JSON document with ArduinoJson:

JsonDocument doc;

doc["sensor"] = "gps";
doc["time"]   = 1351824120;
doc["data"][0] = 48.756080;
doc["data"][1] = 2.302038;

serializeJson(doc, Serial);
// This prints:
// {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}

See the tutorial on arduinojson.org

Sponsors

ArduinoJson is thankful to its sponsors. Please give them a visit; they deserve it!

Programming Electronics Academy

1technophile LArkema

If you run a commercial project that embeds ArduinoJson, think about sponsoring the library's development: it ensures the code that your products rely on stays actively maintained. It can also give your project some exposure to the makers' community.

If you are an individual user and want to support the development (or give a sign of appreciation), consider purchasing the book Mastering ArduinoJson â¤, or simply cast a star â­.