import ballerina/io;public function main() {
    json j1 = "Apple";
    io:println(j1);
    json j2 = 5.36;
    io:println(j2);
    json j3 = true;
    io:println(j3);
    json j4 = false;
    io:println(j4);
    json j5 = null;
    json j6 = { name: "apple", color: "red", price: j2 };
    io:println(j6);
    json j7 = [1, false, null, "foo", { first: "John", last: "Pala" }];
    io:println(j7);
    json empty = {};    int age = 30;
    json p = { fname: "John", lname: "Stallone", "age": age };
    io:println(p);
    json firstName = p.fname;
    io:println(firstName);
    json lastName = p["lname"];
    io:println(lastName);
    p.lname = "Silva";
    p["age"] = 31;
    io:println(p);
    json p2 = {
         fname: "Peter",
         lname: "Stallone",
         "age": age,
         address: {
             line: "20 Palm Grove",
             city: "Colombo 03",
             country: "Sri Lanka"
         }
    };
    io:println(p2);    p2.address.province = "Western";
    io:println(p2);
}

JSON

JSON is a textual format for representing a collection of values: a simple value (string, number, “true”, “false”, “null”), an array of values, or an object. Ballerina has a single type named json that can represent any JSON value. Thus, json is a built-in union type in Ballerina that can contain values of types nil (as the null value), boolean, int, float, decimal, string, json[] and map<json>.

import ballerina/io;
public function main() {
    json j1 = "Apple";
    io:println(j1);

Create a JSON string value.

    json j2 = 5.36;
    io:println(j2);

Create a JSON number value.

    json j3 = true;
    io:println(j3);

Create a JSON true value.

    json j4 = false;
    io:println(j4);

Create a JSON false value.

    json j5 = null;

Create a JSON null value.

    json j6 = { name: "apple", color: "red", price: j2 };
    io:println(j6);

Create a JSON Objects.

    json j7 = [1, false, null, "foo", { first: "John", last: "Pala" }];
    io:println(j7);

Create a JSON Array. They are arrays of any JSON value.

    json empty = {};

Create an empty JSON Object.

    int age = 30;
    json p = { fname: "John", lname: "Stallone", "age": age };
    io:println(p);

Create a JSON object. Keys can be defined with or without quotes. Values can be any expression.

    json firstName = p.fname;
    io:println(firstName);

You can access the object values by using dot (.) notation or array index notation.

    json lastName = p["lname"];
    io:println(lastName);

Array index notation allows you to use any string valued expression as the index.

    p.lname = "Silva";
    p["age"] = 31;
    io:println(p);

You can add or change object values.

    json p2 = {
         fname: "Peter",
         lname: "Stallone",
         "age": age,
         address: {
             line: "20 Palm Grove",
             city: "Colombo 03",
             country: "Sri Lanka"
         }
    };
    io:println(p2);

Create a Nested JSON object.

    p2.address.province = "Western";
    io:println(p2);
}
# To run this sample, navigate to the directory that contains the
# `.bal` file and execute the `ballerina run` command.
$ ballerina run json.bal
Apple
5.36
true
false
{"name":"apple", "color":"red", "price":5.36}
[1, false, null, "foo", {"first":"John", "last":"Pala"}]
{"fname":"John", "lname":"Stallone", "age":30}
John
Stallone
{"fname":"John", "lname":"Silva", "age":31}
{"fname":"Peter", "lname":"Stallone", "age":30,
    "address":{"line":"20 Palm Grove", "city":"Colombo 03", "country":"Sri Lanka"}}
{"fname":"Peter", "lname":"Stallone", "age":30,
    "address":{"line":"20 Palm Grove", "city":"Colombo 03",
        "country":"Sri Lanka","province":"Western"}}