import ballerina/io;
import ballerina/log;
function getFields(map<json> rec) returns [string[], string[]] {
    string[] fields = [];
    foreach var recordField in rec {
        fields[fields.length()] = recordField.toString();
    }
    return [rec.keys(), fields];
}
function writeCsv(json[] content, string path) returns @tainted error? {
    io:WritableCSVChannel csvch = check io:openWritableCsvFile(path);
    int recIndex = 0;
    int recLen = content.length();
    while (recIndex < recLen) {
        [string[], string[]] result = getFields(<map<json>>content[recIndex]);
        var [headers, fields] = result;
        if (recIndex == 0) {
            check csvch.write(headers);
        }
        check csvch.write(fields);
        recIndex = recIndex + 1;
    }
}public function main() {
    json sample = {
        "employees": {
            "employee": [
                {
                    "id": "1",
                    "firstName": "Tom",
                    "lastName": "Cruise",
                    "photo": "https://ballerina-team/profile/3737.jpg"
                },
                {
                    "id": "2",
                    "firstName": "Maria",
                    "lastName": "Sharapova",
                    "photo": "https://ballerina-team/profile/5676.jpg"
                },
                {
                    "id": "3",
                    "firstName": "James",
                    "lastName": "Bond",
                    "photo": "https://ballerina-team/profile/6776.jpg"
                }
            ]
        }
    };
    string path = "./files/sample.csv";
    var result = writeCsv(<json[]>sample.employees.employee, path);
    if (result is error) {
        log:printError("Error occurred while writing csv record :",
                        err = result);
    } else {
        io:println("json record successfully transformed to a csv, file could" +
                    " be found in " + path);
    }
}

JSON to CSV Transform

This sample demonstrates how a json array can be transformed to CSV.

import ballerina/io;
import ballerina/log;
function getFields(map<json> rec) returns [string[], string[]] {
    string[] fields = [];
    foreach var recordField in rec {
        fields[fields.length()] = recordField.toString();
    }
    return [rec.keys(), fields];
}

Convert a map<json> value to an array of Strings. Return the result as a tuple, which contains the headers and fields.

function writeCsv(json[] content, string path) returns @tainted error? {
    io:WritableCSVChannel csvch = check io:openWritableCsvFile(path);
    int recIndex = 0;
    int recLen = content.length();
    while (recIndex < recLen) {
        [string[], string[]] result = getFields(<map<json>>content[recIndex]);
        var [headers, fields] = result;
        if (recIndex == 0) {

Writes json content to CSV.

            check csvch.write(headers);
        }
        check csvch.write(fields);
        recIndex = recIndex + 1;
    }
}

We ignore the result as this would mean a nil return

public function main() {
    json sample = {
        "employees": {
            "employee": [
                {
                    "id": "1",
                    "firstName": "Tom",
                    "lastName": "Cruise",
                    "photo": "https://ballerina-team/profile/3737.jpg"
                },
                {
                    "id": "2",
                    "firstName": "Maria",
                    "lastName": "Sharapova",
                    "photo": "https://ballerina-team/profile/5676.jpg"
                },
                {
                    "id": "3",
                    "firstName": "James",
                    "lastName": "Bond",
                    "photo": "https://ballerina-team/profile/6776.jpg"
                }
            ]
        }
    };

The sample json, which will be written.

    string path = "./files/sample.csv";

Writes JSON into a CSV.

    var result = writeCsv(<json[]>sample.employees.employee, path);
    if (result is error) {
        log:printError("Error occurred while writing csv record :",
                        err = result);
    } else {
        io:println("json record successfully transformed to a csv, file could" +
                    " be found in " + path);
    }
}

Specifies the JSON array, which should be transformed into CSV. Also, provides the location the CSV should be written.

# To run this sample, navigate to the directory that contains the
# `.bal` file, and execute the `ballerina run` command.
$ ballerina run json_csv.bal
json record successfully transformed to a csv, file could be found in ./files/sample.csv