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);
    io:println(j6.toJsonString());
    json j7 = [1, false, null, "foo", { first: "John", last: "Pala" }];
    io:println(j7.toJsonString());
    json j8 = checkpanic j5.mergeJson(j7);
    io:println(j8.toJsonString());
    json j9 = { name: "Anne", age: null, marks: { math: 90, language: 95 } };
    json j10 = { name: (), age: 20, marks: { physics: 85 } };
    json j11 = checkpanic j9.mergeJson(j10);
    io:println(j11.toJsonString());
    io:println(j9 === j11);
    json|error j12 = j2.mergeJson(j3);
    io:println(j12);
    string s = j6.toJsonString();
    json j13 = checkpanic s.fromJsonString();
    io:println(j13.toJsonString());
    io:println(j6 == j13);
}

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 the 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);

Creates a JSON Object. This is equivalent to a map<json>.

    io:println(j6.toJsonString());

The .toJsonString() defined for json values returns a string that represents the value in JSON format.

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

Create a JSON Array. This is equivalent to a json[].

    json j8 = checkpanic j5.mergeJson(j7);
    io:println(j8.toJsonString());

The .mergeJson() method can be used to merge two json values. If either of the two values is null, the result of .mergeJson() is the other value.

    json j9 = { name: "Anne", age: null, marks: { math: 90, language: 95 } };
    json j10 = { name: (), age: 20, marks: { physics: 85 } };
    json j11 = checkpanic j9.mergeJson(j10);
    io:println(j11.toJsonString());

.mergeJson() can also be used to merge two JSON objects. Where both json values are mappings, for each entry in the second, if an entry is not present in the first by the same key, the entry is added to the first mapping. If there is already a field by the same key in the first mapping, .mergeJson() is called recursively.

    io:println(j9 === j11);

Reference equality checks between j9 and j11 evaluate to true since the j9 itself is updated and returned if the merge is successful.

    json|error j12 = j2.mergeJson(j3);
    io:println(j12);

.mergeJson() returns an error if the values cannot be merged. For example, attempting to merge a mapping value with a non-mapping value or two non-mapping values where neither is null.

    string s = j6.toJsonString();
    json j13 = checkpanic s.fromJsonString();
    io:println(j13.toJsonString());

The .fromJsonString() defined on string values attempts parsing the string expected to be in the JSON format and returns the represented JSON value if successful. This method returns an error if the string cannot be parsed.

    io:println(j6 == j13);
}

The value would be equal to the original value from which the string was created.

# 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
{"name":"apple", "color":"red", "price":5.36}
[1, false, null, "foo", {"first":"John", "last":"Pala"}]
[1, false, null, "foo", {"first":"John", "last":"Pala"}]
{"name":"Anne", "age":20, "marks":{"math":90, "language":95, "physics":85}}
true
error {ballerina/lang.value}MergeJsonError message=Cannot merge JSON values of types 'float' and 'boolean'
{"name":"apple", "color":"red", "price":5.36}
true