using jansson api's create json file write data into file but i need new line for very key

293 Views Asked by At

Output:

{"PSM": {"LinkName": "ath6", "LinkType": "WiFi", "PriorityTag": 1, "Pvid": 106}, "SYSCFG": {"LinkName": "ath6", "LinkType": "WiFi", "PriorityTag": 0, "Pvid": 107}, "rbus_event": {"LinkName": "ath6", "LinkType": "WiFi", "PriorityTag": 0, "Pvid": 108}, "SYSEVENT": {"LinkName": "ath6", "LinkType": "WiFi", "PriorityTag": 0, "Pvid": 109}}

Expected Output:

{"PSM": {
"LinkName": "ath6",
 "LinkType": "WiFi", 
 "PriorityTag": 1, 
 "Pvid": 106}, 
"SYSCFG": {
"LinkName": "ath6", 
 "LinkType": "WiFi",
 "PriorityTag": 0,
 "Pvid": 107},
 "rbus_event": {
"LinkName": "ath6",
 "LinkType": "WiFi",
 "PriorityTag": 0,
 "Pvid": 108},
 "SYSEVENT": {
"LinkName": "ath6",
 "LinkType": "WiFi",
 "PriorityTag": 0,
 "Pvid": 109}}
1

There are 1 best solutions below

0
On

You get a prettified output of your JSON when encoding with a proper indentation.

If you read a JSON which is not already prettified you have to decode it first and encode it the way you want - something like:

json_t *root = json_loads(input, 0, &error);

if (root) {
    char *dump = json_dumps(root, JSON_INDENT(4));
    
    if (dump) {
        printf("%s", dump);
        free(dump);
        dump = NULL;
    }
}