Back to Examples
- Binding patterns
- Typed binding pattern
- Wildcard binding pattern
- List binding patterns
- Rest binding pattern in list binding pattern
- Mapping binding pattern
- Rest binding pattern in mapping binding pattern
- Error binding pattern
- Rest binding pattern in error binding pattern
- Single use of typed binding patterns
- Single use of typed binding patterns with on fail clause
- Iterative use of typed binding patterns
- List binding pattern in match statement
- Mapping binding pattern in match statement
- Error binding pattern in match statement
- Query expressions
- Sort iterable objects
- Let clause
- Limit clause
- Join iterable objects
- Outer Join clause
- Query tables
- Create tables with a query
- Create maps with a query
- Create streams with a query
- On conflict clause
- Advanced conflict handling
- Iterate over XML with a query
- Nested query expressions
- Destructure records using a query
- Querying streams
- Aggregation
- JSON type
- Access JSON elements
- Access optional JSON elements
- Match statement with maps
- Convert from user-defined type to JSON
- Convert from table and XML to JSON
- Convert from JSON to user-defined type
- Cast JSON to user-defined type
- Resource method typing
- JSON numbers
- JSON to record
- JSON to record with projection
- JSONPath expressions
- Asynchronous function calls
- Named workers
- Sequence diagrams
- Wait for workers
- Strands
- Named worker return values
- Alternate wait
- Multiple wait
- Named workers and futures
- Inter-worker message passing
- Alternate receive
- Multiple receive
- Conditional send
- Inter-worker failure propagation
- Named worker with on fail clause
- Synchronize message passing
- Asynchronize message passing
- Flush
- Fork
Access JSON elements
Ballerina defines certain types as lax types for which static typing rules are less strict. json
is defined to be a lax type along with any map<T>
where T
is a lax type.
For example, field access (.
) and optional field access (?.
), which are generally allowed on records and objects for fields that are defined in the type descriptors, are also allowed additionally on lax types. For such operations, some of the type checkings are moved from compile time to runtime.
The best way of accessing JSON elements is to convert the json
value to a user-defined type.
check Expr
is treated as check val:ensureType(Expr, s)
when the Expr
is a subtype of JSON and the expected type is a subtype of ()|boolean|int|float|decimal|string
. s
is a typedesc value representing the expected type.
import ballerina/io;
import ballerina/lang.value;
public function main() returns error? {
json[] users = [
{
user: {
name: {
firstName: "John",
lastname: "Smith"
},
age: 24
}
},
null
];
// Field access is allowed on the `json` typed variable. However, the return
// type would be a union of `json` and `error`.
json firstUserName = check users[0].user.name;
// This is converted to `check value:ensureType(firstUserName.firstName, string)`.
// As the expected type is correct, the conversion is successful.
string firstName = check firstUserName.firstName;
io:println("Value of first name: " + firstName);
// This is same as above.
firstName = check value:ensureType(firstUserName.firstName, string);
io:println("Value of first name: " + firstName);
}
Run the example as follows.
$ bal run access_json_elements.balValue of first name: JohnValue of first name: John