Creating and Parsing JSON Data with ESP32 IDF

Introduction:When developing with the ESP32, my favorite framework to use is the IDF framework, which is flexible, convenient, and comprehensive. This article utilizes the espressif__json_generator and espressif__json_parser components under the IDF framework to implement the creation and parsing of JSON data format.

1. espressif__jsmn is a dependency and will be automatically downloaded

Creating and Parsing JSON Data with ESP32 IDF

II. Writing the Parsing Example

1. Since there are no official examples available, I will write one for comprehensive testing

#define json_test_str                         \
    "{\n\"str_val\" :    \"JSON Parser\",\n"  \
    "\t\"float_val\" : 2.0,\n"                \
    "\"int_val\" : 2017,\n"                   \
    "\"bool_val\" : false,\n"                 \
    "\"supported_el\" :\t [\"bool\",\"int\",\n" \
    "\"float\",\"str\"\n"                       \
    "\",\"object\",\"array\"],\n"               \
    "\"features\" : { \"objects\":true, "     \
    "\"arrays\":\"yes\"},\n"                  \
    "\"int_64\":109174583252}"\n\nvoid parse_json_example(void) {\n    jparse_ctx_t jctx;\n    int ret = json_parse_start(&jctx, json_test_str, strlen(json_test_str));\n\n    char str_val[64];\n    int int_val, num_elem;\n    int64_t int64_val;\n    bool bool_val;\n    float float_val;\n\n    json_obj_get_string(&jctx, \"str_val\", str_val, sizeof(str_val));\n    ESP_LOGI(SYSTEN_TAG, \"JSON Parser :%s\", str_val);\n\n    json_obj_get_float(&jctx, \"float_val\", &float_val);\n    ESP_LOGI(SYSTEN_TAG, \"float_val :%f\", float_val);\n\n    json_obj_get_int(&jctx, \"int_val\", &int_val);\n    ESP_LOGI(SYSTEN_TAG, \"int_val :%d\", int_val);\n\n    json_obj_get_bool(&jctx, \"bool_val\", &bool_val);\n    ESP_LOGI(SYSTEN_TAG, \"bool_val :%d\", bool_val);\n\n    json_obj_get_array(&jctx, \"supported_el\", &num_elem);\n\n    for (int i = 0; i < num_elem; ++i) {\n        json_arr_get_string(&jctx, i, str_val, sizeof(str_val));\n        ESP_LOGI(SYSTEN_TAG, \"supported_el[%d] :%s\", i, str_val);\n    }\n    json_obj_leave_array(&jctx);\n\n    json_obj_get_object(&jctx, \"features\");\n    json_obj_get_bool(&jctx, \"objects\", &bool_val);\n    ESP_LOGI(SYSTEN_TAG, \"objects :%d\", bool_val);\n    json_obj_get_string(&jctx, \"arrays\", str_val, sizeof(str_val));\n    ESP_LOGI(SYSTEN_TAG, \"arrays :%s\", str_val);\n    json_obj_leave_object(&jctx);\n\n    json_obj_get_int64(&jctx, \"int_64\", &int64_val);\n    ESP_LOGI(SYSTEN_TAG, \"int64_val :%lld\", int64_val);\n\n    json_parse_end(&jctx);\n}

2. Call parse_json_example where needed to parse and print each item

Creating and Parsing JSON Data with ESP32 IDF

III. Writing the Creation Example

1. Since there are no official examples available, I will write one for comprehensive testing

// Custom flush callback function: used to print or send data\nvoid json_flush_callback(const char *buffer, void *priv_data) {\n    printf(%s\r\n, buffer);  // Print to console\n}\n\nvoid generator_json_example(void) {\n    char buffer[256];  // Used as JSON building buffer\n\n    json_gen_str_t jstr;\n\n    // Initialize JSON generator\n    json_gen_str_start(&jstr, buffer, 256, json_flush_callback, NULL);\n\n    // Start object\n    json_gen_start_object(&jstr);\n\n    // Add key-value pairs\n    json_gen_obj_set_string(&jstr, \"name\", \"ESP32\");\n    json_gen_obj_set_float(&jstr, \"temp\", 25.5f);\n    json_gen_obj_set_bool(&jstr, \"online\", true);\n\n    // Add array\n    json_gen_push_array(&jstr, \"sensors\");\n    json_gen_arr_set_int(&jstr, 10);\n    json_gen_arr_set_int(&jstr, 20);\n    json_gen_arr_set_int(&jstr, 30);\n    json_gen_pop_array(&jstr);\n\n    // Nested object\n    json_gen_push_object(&jstr, \"location\");\n    json_gen_obj_set_float(&jstr, \"lat\", 32.021779f);\n    json_gen_obj_set_float(&jstr, \"lon\", 118.747673f);\n    json_gen_pop_object(&jstr);\n\n    // End object\n    json_gen_end_object(&jstr);\n\n    // Finally flush remaining content\n    json_gen_str_end(&jstr);\n}

2. Call generator_json_example where needed to print the constructed JSON

Creating and Parsing JSON Data with ESP32 IDF

3. Explanation of the json_gen_end_object(&jstr); function

This function is essentially the last step in creating JSON data, adding a “}” to the end of the data.

4. Explanation of the json_gen_str_end(&jstr); function

This is just to trigger the callback function to check if the constructed data is correct, and it will clear the memory used for construction. In actual product use, this function may not be necessary.

Leave a Comment