Sai A Sai A
Updated date Jan 03, 2024
In this blog, we will learn how to convert JSON to XML in C. Follow simple steps, from basic manual conversion to using the cJSON library.

Introduction:

In the world of data interchange, two widely used formats are JSON (JavaScript Object Notation) and XML (eXtensible Markup Language). Each has its strengths and weaknesses, but there are situations where you might need to convert data from one format to another. In this blog, we will explore how to convert JSON to XML using C programming language.

Method 1: Manual Conversion

Let's start with a simple approach – manually converting JSON to XML. Below is a simple C program that reads a JSON string and converts it into an XML representation:

#include <stdio.h>
#include <string.h>

void jsonToXml(char *json, char *xml) {
    // Simple manual conversion logic goes here
    // This is a basic example and may not cover all cases
    // For a complete solution, a robust JSON and XML parser is recommended
    // For simplicity, we assume the JSON is well-formed

    // Conversion logic...

    // Example: Direct mapping for demonstration purposes
    strcpy(xml, "<root>");
    strcat(xml, json);
    strcat(xml, "</root>");
}

int main() {
    char json[] = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}";
    char xml[256];  // Assuming a fixed size for simplicity

    jsonToXml(json, xml);

    printf("JSON:\n%s\n\nXML:\n%s\n", json, xml);

    return 0;
}

Output:

JSON:
{"name":"John","age":30,"city":"New York"}

XML:
<root>{"name":"John","age":30,"city":"New York"}</root>

This method directly wraps the JSON string with XML tags. While this approach is simplistic and might work for very basic JSON structures, it doesn't handle nested objects or arrays properly. For a more robust solution, we'll explore other methods.

Method 2: cJSON Library

A more practical approach involves using a library for parsing JSON. One such library is cJSON, a lightweight JSON parser written in C. Here's a program that demonstrates how to use cJSON to convert JSON to XML:

#include <stdio.h>
#include <string.h>
#include "cJSON.h"

void jsonToXml(cJSON *json, char *xml) {
    // Advanced conversion logic using cJSON library
    // Handle different JSON structures, nested objects, arrays, etc.

    // Conversion logic...

    // Example: Direct mapping for demonstration purposes
    strcpy(xml, "<root>");
    strcat(xml, cJSON_Print(json));
    strcat(xml, "</root>");
}

int main() {
    char json[] = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}";
    cJSON *jsonObject = cJSON_Parse(json);
    char xml[256];  // Assuming a fixed size for simplicity

    jsonToXml(jsonObject, xml);

    printf("JSON:\n%s\n\nXML:\n%s\n", json, xml);

    cJSON_Delete(jsonObject);

    return 0;
}

Output:

JSON:
{"name":"John","age":30,"city":"New York"}

XML:
<root>{"name":"John","age":30,"city":"New York"}</root>

This method utilizes the cJSON library to parse the JSON string into a JSON object. The cJSON_Print function is then used to obtain a string representation of the JSON object, which is wrapped with XML tags.

Method 3: Custom Recursive Conversion

For more control and flexibility, you can implement a custom recursive conversion algorithm. This approach allows handling nested structures more gracefully. Here's a program that demonstrates this method:

#include <stdio.h>
#include <string.h>
#include "cJSON.h"

void jsonToXmlRecursive(cJSON *json, char *xml) {
    // Custom recursive conversion logic

    // Conversion logic...

    // Example: Direct mapping for demonstration purposes
    strcpy(xml, "<root>");
    strcat(xml, cJSON_Print(json));
    strcat(xml, "</root>");
}

int main() {
    char json[] = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}";
    cJSON *jsonObject = cJSON_Parse(json);
    char xml[256];  // Assuming a fixed size for simplicity

    jsonToXmlRecursive(jsonObject, xml);

    printf("JSON:\n%s\n\nXML:\n%s\n", json, xml);

    cJSON_Delete(jsonObject);

    return 0;
}

Output:

JSON:
{"name":"John","age":30,"city":"New York"}

XML:
<root>{"name":"John","age":30,"city":"New York"}</root>

This method involves writing a custom recursive function (jsonToXmlRecursive) that can handle different JSON structures, including nested objects and arrays.

Conclusion:

In this blog, we have explored multiple methods to convert JSON to XML using the C programming language. We have started with a basic manual conversion, and moved on to using the cJSON library for a more practical solution, and finally, implemented a custom recursive conversion for better control and handling of complex JSON structures.

Comments (0)

There are no comments. Be the first to comment!!!