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
JSON to record conversion with projection
The data.jsondata
library provides multiple APIs to selectively convert required fields from JSON data in the form of a string
, byte[]
, byte-block-stream
, and json
to a Ballerina record.
For more information on the underlying module, see the data.jsondata
module.
import ballerina/data.jsondata;
import ballerina/io;
// Define a closed record type to capture the required fields from the JSON content.
type Book record {|
string name;
string author;
|};
json jsonContent = {
"name": "Clean Code",
"author": "Robert C. Martin",
"year": 2008,
"publisher": "Prentice Hall"
};
string jsonStr = string `
{
"name": "The Pragmatic Programmer",
"author": "Andrew Hunt, David Thomas",
"year": 1999,
"publisher": "Addison-Wesley"
}`;
public function main() returns error? {
// Based on the expected type, it selectively converts the JSON content to the record type.
Book book = check jsondata:parseAsType(jsonContent);
io:println(book);
// Based on the expected type, it selectively converts the JSON string to the record type.
Book book2 = check jsondata:parseString(jsonStr);
io:println(book2);
}
$ bal run json_to_record_with_projection.bal{"name":"Clean Code","author":"Robert C. Martin"}{"name":"The Pragmatic Programmer","author":"Andrew Hunt, David Thomas"}
PreviousJSON to record
NextJSONPath expressions