Swan Lake Beta3 is here.
import ballerina/io;

// Closed type.
type ClosedCoord record {|
    float x;
    float y;
|};

// Open type, can have additional `anydata` fields.
type OpenCoord record {
    float x;
    float y;
};

public function main() {
    ClosedCoord a = {x: 1.0, y: 2.0};
    // Nothing to do.
    json j = a;

    io:println(j);

    OpenCoord b = {x: 1.0, y: 2.0, "z": "city"};
    // Use `toJson` to convert `anydata` to `json`.
    // Usually happens automatically.
    json k = b.toJson();

    io:println(k);
}

Converting From User-Defined Type to JSON

Conversion from json value to JSON format is straightforward. Converting from application-specific, user-defined subtype of anydata to json is also possible. In many cases, this is a no-op: user-defined type will be a subtype of json as well as of anydata. With tables, XML or records open to anydata, use toJson to convert anydata to json. APIs that generate JSON typically accept anydata and automatically apply toJson.

import ballerina/io;
type ClosedCoord record {|
    float x;
    float y;
|};

Closed type.

type OpenCoord record {
    float x;
    float y;
};

Open type, can have additional anydata fields.

public function main() {
    ClosedCoord a = {x: 1.0, y: 2.0};
    json j = a;

Nothing to do.

    io:println(j);
    OpenCoord b = {x: 1.0, y: 2.0, "z": "city"};
    json k = b.toJson();

Use toJson to convert anydata to json. Usually happens automatically.

    io:println(k);
}
bal run converting_from_user_defined_type_to_json.bal
{"x":1.0,"y":2.0}
{"x":1.0,"y":2.0,"z":"city"}
import ballerina/io;

// Closed type.
type ClosedCoord record {|
    float x;
    float y;
|};

// Open type, can have additional `anydata` fields.
type OpenCoord record {
    float x;
    float y;
};

public function main() {
    ClosedCoord a = {x: 1.0, y: 2.0};
    // Nothing to do.
    json j = a;

    io:println(j);

    OpenCoord b = {x: 1.0, y: 2.0, "z": "city"};
    // Use `toJson` to convert `anydata` to `json`.
    // Usually happens automatically.
    json k = b.toJson();

    io:println(k);
}

Converting From User-Defined Type to JSON

Conversion from json value to JSON format is straightforward. Converting from application-specific, user-defined subtype of anydata to json is also possible. In many cases, this is a no-op: user-defined type will be a subtype of json as well as of anydata. With tables, XML or records open to anydata, use toJson to convert anydata to json. APIs that generate JSON typically accept anydata and automatically apply toJson.

import ballerina/io;
type ClosedCoord record {|
    float x;
    float y;
|};

Closed type.

type OpenCoord record {
    float x;
    float y;
};

Open type, can have additional anydata fields.

public function main() {
    ClosedCoord a = {x: 1.0, y: 2.0};
    json j = a;

Nothing to do.

    io:println(j);
    OpenCoord b = {x: 1.0, y: 2.0, "z": "city"};
    json k = b.toJson();

Use toJson to convert anydata to json. Usually happens automatically.

    io:println(k);
}
bal run converting_from_user_defined_type_to_json.bal
{"x":1.0,"y":2.0}
{"x":1.0,"y":2.0,"z":"city"}
Subscribe to the newsletter

In the creation of Ballerina, we were inspired by so many technologies. Thank you to all that have come before us (and forgive us if we missed one): Java, Go, C, C++, D, Rust, Haskell, Kotlin, Dart, TypeScript, JavaScript, Python, Perl, Flow, Swift, Elm, RelaxNG, NPM, Crates, Maven, Gradle, Kubernetes, Docker, Envoy, Markdown, GitHub and WSO2.

Cookie Policy

This website uses cookies so that we can provide you with the best user experience. Read our Cookie Policy to find out more.

If you wish to disable cookies you can do so from your browser.

I Understand