Choice of JSON lib - themactep/thingino-firmware GitHub Wiki

Two the most popular JSON libraries for C are JSON-C and cJSON. The latter is smaller and preferable for embedded devices where every byte counts.

cJSON API Equivalents Mapping

### Data Structure


json-c                                       | cJSON                                | Notes
---------------------------------------------|--------------------------------------|-------
json_object *                                | cJSON *                              |

### Memory Management


json-c                                       | cJSON                                | Notes
---------------------------------------------|--------------------------------------|-------
json_object_put(obj)                         | cJSON_Delete(obj)                    | Different memory management model

### Parsing Functions

json-c                                       | cJSON                                | Notes
---------------------------------------------|--------------------------------------|-------
json_object_from_file(path)                  | cJSON_Parse(string)                  | cJSON requires manual file reading

### Object Access Functions

json-c                                       | cJSON                                | Notes
---------------------------------------------|--------------------------------------|-------
json_object_object_get_ex(obj, key, &result) | cJSON_GetObjectItem(obj, key)        | Different return pattern

### Type Checking Functions

json-c                                       | cJSON                                | Notes
---------------------------------------------|--------------------------------------|-------
json_object_is_type(obj, json_type_string)   | cJSON_IsString(obj)                  | More specific functions
json_object_is_type(obj, json_type_boolean)  | cJSON_IsBool(obj)                    |
json_object_is_type(obj, json_type_int)      | cJSON_IsNumber(obj)                  |
json_object_is_type(obj, json_type_double)   | cJSON_IsNumber(obj)                  |
json_object_is_type(obj, json_type_object)   | cJSON_IsObject(obj)                  |
json_object_is_type(obj, json_type_array)    | cJSON_IsArray(obj)                   |

### Value Extraction Functions

json-c                                       | cJSON                                | Notes
---------------------------------------------|--------------------------------------|-------
json_object_get_string(obj)                  | cJSON_GetStringValue(obj)            |
json_object_get_boolean(obj)                 | cJSON_IsTrue(obj)                    | Returns boolean
json_object_get_int(obj)                     | cJSON_GetNumberValue(obj)            | Returns double, cast needed
json_object_get_int64(obj)                   | cJSON_GetNumberValue(obj)            | Returns double, cast needed
json_object_get_double(obj)                  | cJSON_GetNumberValue(obj)            |

### Object Creation Functions

json-c                                       | cJSON                                | Notes
---------------------------------------------|--------------------------------------|-------
json_object_new_object()                     | cJSON_CreateObject()                 |
json_object_new_string(str)                  | cJSON_CreateString(str)              |
json_object_new_boolean(val)                 | cJSON_CreateBool(val)                |
json_object_new_int(val)                     | cJSON_CreateNumber(val)              |
json_object_new_int64(val)                   | cJSON_CreateNumber(val)              |
json_object_new_double(val)                  | cJSON_CreateNumber(val)              |
json_object_new_array()                      | cJSON_CreateArray()                  |

### Object Manipulation Functions

json-c                                       | cJSON                                | Notes
---------------------------------------------|--------------------------------------|-------
json_object_object_add(obj, key, val)        | cJSON_AddItemToObject(obj, key, val) |
json_object_array_add(arr, val)              | cJSON_AddItemToArray(arr, val)       |
json_object_object_del(obj, key)             | cJSON_DeleteItemFromObject(obj, key) |

### Array Functions

json-c                                       | cJSON                                | Notes
---------------------------------------------|--------------------------------------|-------
json_object_array_length(arr)                | cJSON_GetArraySize(arr)              |
json_object_array_get_idx(arr, idx)          | cJSON_GetArrayItem(arr, idx)         |

### Serialization Functions

json-c                                       | cJSON                                | Notes
---------------------------------------------|--------------------------------------|-------
json_object_to_json_string_ext(obj, flags)   | cJSON_Print(obj)                     | Always pretty prints