C:
Working with JSON

How to:

To work with JSON in C, you’ll typically use a library like jansson or json-c due to C’s lack of built-in support for JSON. Here, we’ll focus on jansson for its ease of use and active maintenance. First, install the library (e.g., using a package manager like apt on Ubuntu: sudo apt-get install libjansson-dev).

Let’s start by parsing a JSON string and accessing its contents:

#include <jansson.h>
#include <stdio.h>

int main() {
    const char *json_string = "{\"name\":\"John Doe\",\"age\":30}";
    json_error_t error;
    json_t *root = json_loads(json_string, 0, &error);
    
    if(!root) {
        fprintf(stderr, "error: on line %d: %s\n", error.line, error.text);
        return 1;
    }
    
    const char *name;
    int age;
    json_unpack(root, "{s:s, s:i}", "name", &name, "age", &age);
    
    printf("Name: %s\nAge: %d\n", name, age);
    
    json_decref(root);
    return 0;
}

Sample Output:

Name: John Doe
Age: 30

Next, creating and outputting a JSON object:

#include <jansson.h>
#include <stdio.h>

int main() {
    json_t *root = json_object();
    json_object_set_new(root, "name", json_string("Jane Doe"));
    json_object_set_new(root, "age", json_integer(25));
    
    char *json_dump = json_dumps(root, JSON_ENCODE_ANY);
    printf("%s\n", json_dump);
    
    free(json_dump);
    json_decref(root);
    return 0;
}

Sample Output:

{"name": "Jane Doe", "age": 25}

These examples demonstrate the basics of loading a JSON string, unpacking its values, creating a new JSON object, and then outputting it as a string.

Deep Dive

The need to work with JSON in C springs from the web’s adoption of JSON as a primary format for data interchange. JSON’s simplicity and efficiency made it quickly outpace XML, despite C’s initial absence in direct support for JSON manipulation. Early solutions involved manual string manipulation - error-prone and inefficient. Libraries like jansson and json-c emerged to fill this gap, providing robust APIs for JSON parsing, construction, and serialization.

While jansson offers simplicity and ease of use, json-c might appeal to those looking for a broader feature set. Nevertheless, alternatives like parsing libraries in C++ offer more sophisticated abstractions, thanks to that language’s more complex data structures and standard library support. However, when working in environments where C is the preferred or required language - such as embedded systems or when interfacing with existing C libraries - using jansson or json-c becomes indispensable.

It’s also worth noting that working with JSON in C involves a deeper understanding of memory management, as these libraries frequently return dynamically allocated objects requiring explicit deallocation. This challenges programmers to balance convenience with the responsibility of preventing memory leaks, a crucial aspect of crafting efficient C code.