import ballerina/io;

public function main() returns error? {
    // Initializes the JSON file path and content.
    string jsonFilePath = "./files/jsonFile.json";
    json jsonContent = {"Store": {
            "@id": "AST",
            "name": "Anne",
            "address": {
                "street": "Main",
                "city": "94"
            },
            "codes": ["4", "8"]
        }};

    // Writes the given JSON to a file.
    check io:fileWriteJson(jsonFilePath, jsonContent);
    // If the write operation was successful, then, performs a read operation to read the JSON content.
    json readJson = check io:fileReadJson(jsonFilePath);
    io:println(readJson);
}

Read/Write JSON

This sample demonstrates how to read JSON content from a file and write JSON content to a file using the character channel, readJson(), and writeJson() of the I/O API.

For more information on the underlying module, see the IO module.

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

Initializes the JSON file path and content.

    check io:fileWriteJson(jsonFilePath, jsonContent);

Writes the given JSON to a file.

    json readJson = check io:fileReadJson(jsonFilePath);
    io:println(readJson);
}

If the write operation was successful, then, performs a read operation to read the JSON content.

bal run io_json.bal
{"Store":{"@id":"AST","name":"Anne","address":{"street":"Main","city":"94"},"codes":["4","8"]}}