One of the standout features of ballerina from most of the other programming language is the first class support for JSON and XML as built-in data types. Here I will be discussing on how to do JSON manipulations with Ballerina with ease.
Defining JSON
According to the definition [1], a JSON can be of the below kinds:- string
- number (integer/float)
- boolean
- Object (key-value pairs, with value can be any JSON)
- Array (of any JSON)
- null
Thus in ballerina also, you can define a JSON of any of the above kinds, and assign it to a json type variable.
// JSON string value. json j1 = "Apple"; // JSON number value. json j2 = 5.36; json j3 = 4; // JSON true value. json j4 = true; // JSON false value. json j5 = false; //JSON Objects. json j6 = {fname:"John", lname:"Doe", age:34}; //JSON Arrays. They are arrays of any JSON value. json j7 = [1, false, null, "foo", {first:"John", last:"Doe"}]; // JSON null value. json j8= null;
JSON complex objects
Like I've mentioned earlier (j6) we can define complex json objects in ballerina, using JSON literal. Here a JSON litera refers to the literal starts with '{' and ends with '}'. In a JSON Literal, keys are always treated as static entries. for example, in the below JSON literal the key 'fname' would create a filed called 'fname' in the JSON object. It will not treat as a variable, and wont be evaluating the value of any variable. Also, inside a JSON literal, there can be nested literals as well. 'address' as a sample for this, where it is another JSON object.
json info = {fname:"John", lname:"Goe", age:34, address:{street:"Main st", city:"Colombo": country:"Sri Lanka"}};
How can we access each element of a complex JSON? Answer is simple, we can it any of the below two methods, using field-based-access. Suppose we ned to access the Country:
json country = info.address.country;Or
json country = info["address"]["country"];
Note that in both occasions, the returned value is again a JSON. It is a string kind JSON. To get the string representation of it, we can cast it to string:
var countryStr, e = (string) country;
JSON Arrays
JSON Arrays can also be created using the usual array literals (starts with '[' and ends with ']') as described in the earlier section. Again similar to Object type JSON, in arrays also, there can be nested array literals, as well as JSON literals for complex JSON objects:json array = [1, false, null, "foo", {first:"John", last:"Doe"}];
Hot access an element of the array using index? It is pretty much same way as you access any other array, using index-based access:
json info = array[4];
Again similar to accessing JSON objects, the returned value is again a JSON.
NOTE:
In Ballerina you can also define an array of json values as well:
json[] arrayOfJson = [1, false, null, "foo", {first:"John", last:"Doe"}];
In this case 'arrayOfJson' behaves in the same way as the 'array' we defined earlier, when it comes to accessing elements using index. However, arrayOfJson can hold only array type JSON, while 'array' can hold any type of JSON.
Accessing arrays in objects/ objects in arrays
In the most cases, a complex JSON may have al sorts of values in it, With nested arrays and objects. Thus usually need to access an array element of an object, or a key value pair of an object residing in an array. See the sample below:
json array = [1, false, null, "foo", {name:{first:"John", last:"Doe"}}];
Suppose we need to access first name of the complex element at the 4th index of the array. Then we can use a combination of both index-based-access notation and field-based access notations:
json firstName = array[4].name.first;
Similarly, we can use them in any combination.
References
[1] http://json.org/Wrote by Supun Setunga