-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5287 from SasinduDilshara/jsonpath-bbe
Add BBE for jsonpath expressions
- Loading branch information
Showing
5 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import ballerina/data.jsondata; | ||
import ballerina/io; | ||
|
||
public function main() returns error? { | ||
json books = [ | ||
{ | ||
title: "The Great Gatsby", | ||
author: "F. Scott Fitzgerald", | ||
price: 100, | ||
year: 1925 | ||
}, | ||
{ | ||
title: "To Kill a Mockingbird", | ||
author: "Harper Lee", | ||
price: 72.5, | ||
year: 1960 | ||
}, | ||
{ | ||
title: "1984", | ||
author: "George Orwell", | ||
price: 90, | ||
year: 1949 | ||
} | ||
]; | ||
|
||
// JSONPath expression to get the list of titles in the books array. | ||
json titles = check jsondata:read(books, `$..title`); | ||
io:println(titles); | ||
|
||
// JSONPath expression to get the list of published years for the | ||
// books that have a price value of more than 80. | ||
json years = check jsondata:read(books, `$..[?(@.price > 80)].year`); | ||
io:println(years); | ||
|
||
// JSONPath expression to get the total sum of the prices of the books. | ||
json sum = check jsondata:read(books, `$..price.sum()`); | ||
io:println(sum); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# JSONPath expressions | ||
|
||
Ballerina supports JSONPath expressions against JSON data through [`ballerina/data.jsondata` module](https://lib.ballerina.io/ballerina/data.jsondata/latest/) module to provide a straightforward method | ||
for navigating the structure of JSON data. | ||
Alternatively, Ballerina supports type based access to JSON data by language itself. | ||
|
||
::: code jsonpath_expressions.bal ::: | ||
|
||
::: out jsonpath_expressions.out ::: | ||
|
||
## Related links | ||
- [JSON type](/learn/by-example/json-type/) | ||
- [Access JSON elements](/learn/by-example/access-json-elements/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
description: This BBE demonstrates JSON path expressions with Ballerina | ||
keywords: ballerina, ballerina by example, bbe, json, jsonpath, jsonpath expressions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
$ bal run jsonpath_expressions.bal | ||
["The Great Gatsby","To Kill a Mockingbird","1984"] | ||
[1925,1949] | ||
262.5 |