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
Casting JSON to user-defined type
In order to access the field in the json
value, the easiest way is to convert the json
value to a user-defined type.
The type-casting can be used to do that. However, if the cast fails, the program panics with an error. The recommended way to do this is by using langlib functions.
Casting to a user-defined type will work on mutable structure only if the inherent type (a structured value has an inherent type, which is a type descriptor that is part of the structured value's runtime value) of that structure is a subtype of the user-defined type.
Casting immutable values will work. However, it does not do numeric conversions.
import ballerina/io;
type Coord record {
float x;
float y;
};
public function main() {
json j = {x: 1.0, y: 2.0};
// Here, the inherent type of `j` is not a subtype of `Coord`.
// Therefore, `j` cannot be directly converted to `Coord`.
// Use `cloneReadOnly()` to create a read-only copy of the mutable value `j`.
// Then, the resulting immutable value can be casted successfully.
json k = j.cloneReadOnly();
Coord c = <Coord> k;
io:println(c.x);
io:println(c.y);
}
$ bal run casting_json_to_user_defined_type.bal1.02.0
Related links
PreviousConvert from JSON to user-defined type
NextResource method typing