import ballerina/io;
import ballerina/xmldata;

public function main() returns error? {
    // Creates a JSON object.
    json jsonObject = {"Store": {
            "@id": "AST",
            "name": "Anne",
            "address": {
                "street": "Main",
                "city": "94"
            },
            "codes": ["4", "8"]
        }};
    // Converts the JSON object to XML using a default `attributePrefix` (i.e., the `@` character)
    // and the default `arrayEntryTag` (i.e., `root`).
    xml? xmlData = check xmldata:fromJson(jsonObject);
    io:println(xmlData);

    if (xmlData is xml) {
        // Converts the XML to JSON object using a default `attributePrefix` (i.e., the `@` character)
        // and the default `preserveNamespaces` (i.e., `true`).
        jsonObject = check xmldata:toJson(xmlData);
        io:println(jsonObject);
    }
}

XML JSON Conversion

The xmldata:fromJson() and xmldata:toJson() functions are used to do the conversions between JSON and XML. For more information on the underlying module, see the XmlData module.

import ballerina/io;
import ballerina/xmldata;
public function main() returns error? {
    json jsonObject = {"Store": {
            "@id": "AST",
            "name": "Anne",
            "address": {
                "street": "Main",
                "city": "94"
            },
            "codes": ["4", "8"]
        }};

Creates a JSON object.

    xml? xmlData = check xmldata:fromJson(jsonObject);
    io:println(xmlData);

Converts the JSON object to XML using a default attributePrefix (i.e., the @ character) and the default arrayEntryTag (i.e., root).

    if (xmlData is xml) {
        jsonObject = check xmldata:toJson(xmlData);
        io:println(jsonObject);
    }
}

Converts the XML to JSON object using a default attributePrefix (i.e., the @ character) and the default preserveNamespaces (i.e., true).

bal run xml_json_conversion.bal
<Store id="AST"><name>Anne</name><address><street>Main</street><city>94</city></address><codes><root>4</root><root>8</root></codes></Store>
{"Store":{"name":"Anne","address":{"street":"Main","city":"94"},"codes":{"root":["4","8"]}},"@id":"AST"}