RepositoryCJPJapan
nopnop2002

nopnop2002/esp-idf-json

Example of JSON Serialize and Deserialize in ESP-IDF

115stars
23forks
0issues
0views

Featured achievements

The project's latest awards

1
TINIX TRENDING·Topic: serializer

#1 Repository of the month

1
TINIX TRENDING·Topic: esp-idf

#1 Repository of the month

1
TINIX TRENDING·Topic: deserialize

#1 Repository of the month

esp-idf-json

Example of JSON Serialize and Deserialize with ESP-IDF.

ESP-IDF includes this cJSON library.
You can use JSON components as standard.
The official documentation for the JSON component is here.

Here is a example by Dave Gamble.

Note for ESP-IDF V6
It was a standard component up to ESP-IDF V5, but became a non-standard component from V6 onwards and was moved to the component registry.
The IDF Component Manager Manifest File (idf_component.yml) is required.

## IDF Component Manager Manifest File
dependencies:
  espressif/cjson:
    version: "^1.7.0"
    rules:
      - if: "idf_version >=6.0"

How to use

git clone https://github.com/nopnop2002/esp-idf-json
cd esp-idf-json/json-basic-object
idf.py flash monitor

About cJSON_Print

From print_value() function, the pointer returned is allocated by cJSON_strdup() and it is returned to the caller.
Buffers returned by cJSON_Print must be freed by the caller.
Please use the proper API (cJSON_free) rather than directly calling stdlib free.

char *my_json_string = cJSON_Print(root);
ESP_LOGI(TAG, "my_json_string\n%s",my_json_string);
cJSON_Delete(root);
cJSON_free(my_json_string);

Basic Structure

  • Array
    Array, like this, have no name.
    Array starts with [ and ends with ].
    root is defined by cJSON_CreateArray.
    element is added by cJSON_AddItemToArray.

    ["abc", 123, true, false, null]
    
  • Object
    Object, like this, have name.
    Object starts with { and ends with }.
    root is defined by cJSON_CreateObject.
    element is added by cJSON_AddItemToObject.

    {
            "version":      "v5.0-dev-3202-ga2d5041492-dirty",
            "cores":        2,
    }
    

Complex structure

  • Object in Array

    ["abc", 123, true, false, null, {
                    "version":      "v5.0-dev-3202-ga2d5041492-dirty",
                    "cores":        2
            }]
    
    [{
                    "id":   0,
                    "version":      "v5.0-dev-3202-ga2d5041492-dirty",
                    "cores":        2
            }, {
                    "id":   1,
                    "version":      "v5.0-dev-3202-ga2d5041492-dirty",
                    "cores":        2
            }]
    
  • Array in Array

    [["abc", 120], ["abc", 121], ["abc", 122], ["abc", 123], ["abc", 124], ["abc", 125], ["abc", 126], ["abc", 127], ["abc", 128], ["abc", 129]]
    
  • Object in Object

    {
            "name": "Jack (\"Bee\") Nimble",
            "format":       {
                    "type": "rect",
                    "width":        1920,
                    "height":       1080,
                    "interlace":    false,
                    "frame rate":   24
            }
    }
    
  • Array in Object

    {
            "version":      "v5.0-dev-3202-ga2d5041492-dirty",
            "cores":        2,
            "array":        ["abc", 123, true, false, null]
    }
    

More Complex structure

  • Object in Array in Object

    {
            "version":      "v5.0-dev-3202-ga2d5041492-dirty",
            "cores":        2,
            "array":  [{
                            "width":        1280,
                            "height":       720
                    }, {
                            "width":        1920,
                            "height":       1080
                    }, {
                            "width":        3840,
                            "height":       2160
                    }]
    }
    
  • Array in Object in Array

    [{
                    "id":   0,
                    "version":      "v5.0-dev-3202-ga2d5041492-dirty",
                    "cores":        2,
                    "array":        ["abc", 123, true, false, null]
            }, {
                    "id":   1,
                    "version":      "v5.0-dev-3202-ga2d5041492-dirty",
                    "cores":        2,
                    "array":        ["abc", 123, true, false, null]
            }]
    

How to Serialize

Serialization starts from the lowest level.
No matter how complex the structure, there are only objects and arrays.

  • Target structure

    {
            "version":      "v5.0-dev-3202-ga2d5041492-dirty",
            "cores":        2,
            "array":  [{
                            "width":        1280,
                            "height":       720
                    }, {
                            "width":        1920,
                            "height":       1080
                    }, {
                            "width":        3840,
                            "height":       2160
                    }]
    }
    
    • Create 3 objects
      This is simple object.

                      {
                              "width":        1280,
                              "height":       720
                      }
      
                      {
                              "width":        1920,
                              "height":       1080
                      }
      
                      {
                              "width":        3840,
                              "height":       2160
                      }
      
      
    • Create array
      This is object in array.

                     [{
                              "width":        1280,
                              "height":       720
                      }, {
                              "width":        1920,
                              "height":       1080
                      }, {
                              "width":        3840,
                              "height":       2160
                      }]
      
    • Create object as root
      This is array in object.

      {
              "version":      "v5.0-dev-3202-ga2d5041492-dirty",
              "cores":        2,
              "array":  [{
                              "width":        1280,
                              "height":       720
                      }, {
                              "width":        1920,
                              "height":       1080
                      }, {
                              "width":        3840,
                              "height":       2160
                      }]
      }
      
  • Target structure

    [{
                    "id":   0,
                    "version":      "v5.0-dev-3202-ga2d5041492-dirty",
                    "cores":        2,
                    "array":        ["abc", 123, true, false, null]
            }, {
                    "id":   1,
                    "version":      "v5.0-dev-3202-ga2d5041492-dirty",
                    "cores":        2,
                    "array":        ["abc", 123, true, false, null]
            }]
    
    • Create 2 arrays
      This is simple array.

      ["abc", 123, true, false, null]
      
      ["abc", 123, true, false, null]
      
    • Create 2 objects
      This is array in object.

              {
                      "id":   1,
                      "version":      "v5.0-dev-3202-ga2d5041492-dirty",
                      "cores":        2,
                      "array":        ["abc", 123, true, false, null]
              }
      
              {
                      "id":   0,
                      "version":      "v5.0-dev-3202-ga2d5041492-dirty",
                      "cores":        2,
                      "array":        ["abc", 123, true, false, null]
              }
      
    • Create array as root
      This is object in array.

      [{
                      "id":   0,
                      "version":      "v5.0-dev-3202-ga2d5041492-dirty",
                      "cores":        2,
                      "array":        ["abc", 123, true, false, null]
              }, {
                      "id":   1,
                      "version":      "v5.0-dev-3202-ga2d5041492-dirty",
                      "cores":        2,
                      "array":        ["abc", 123, true, false, null]
              }]
      

JSON Data Set Sample

This is a sample JSON data set.
https://opensource.adobe.com/Spry/samples/data_region/JSONDataSetSample.html

Similar Projects

Discover other similar projects and AI models.

RepositoryC
HeyWillow/willow

Open source, local, and self-hosted Amazon Echo/Google Home competitive Voice Assistant alternative

3.1k
Nov 1, 2026
RepositoryC
RavenSystem/esp-homekit-devices

Advanced firmware to add native Apple HomeKit and custom configurations, compatible with any SoC based on ESP32, ESP32-S, ESP32-C and ESP8266 series. (Shelly, Sonoff, Electrodragon, Tuya...)

3.0k
Nov 8, 2026
RepositoryC++
espressif/arduino-esp32

Arduino core for the ESP32 family of SoCs

17.8k
Dec 1, 2026
RepositoryGo
json-iterator/go

A high-performance 100% compatible drop-in replacement of "encoding/json"

13.8k
May 27, 2024
RepositoryC++
thesolarnomad/lora-serialization

LoraWAN serialization/deserialization library for The Things Network

166
Mar 27, 2026
RepositoryRust
ruvnet/RuView

π RuView turns commodity WiFi signals into real-time spatial intelligence, vital sign monitoring, and presence detection — all without a single pixel of video.

106.0k
Dec 3, 2026
RepositoryPython
microsoft/markitdown

Python tool for converting files and office documents to Markdown.

199.6k
Dec 3, 2026
RepositoryTypeScript
microsoft/vscode

Visual Studio Code

195.9k
Nov 30, 2026
RepositoryC
lvgl/lvgl

LVGL is a free, full-featured embedded UI library for small MCUs and 3D-capable MPUs, enhanced by LVGL Pro, a professional editor and tooling.

25.3k
Nov 27, 2026