Skip to content

Commit

Permalink
Merge pull request #5287 from SasinduDilshara/jsonpath-bbe
Browse files Browse the repository at this point in the history
Add BBE for jsonpath expressions
  • Loading branch information
SasinduDilshara committed Apr 15, 2024
2 parents 7f307b8 + 90786a8 commit 73b17ba
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 0 deletions.
7 changes: 7 additions & 0 deletions examples/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,13 @@
"verifyBuild": true,
"verifyOutput": true,
"isLearnByExample": true
},
{
"name": "JSONPath expressions",
"url": "jsonpath-expressions",
"verifyBuild": true,
"verifyOutput": true,
"isLearnByExample": true
}
]
},
Expand Down
38 changes: 38 additions & 0 deletions examples/jsonpath-expressions/jsonpath_expressions.bal
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);
}
13 changes: 13 additions & 0 deletions examples/jsonpath-expressions/jsonpath_expressions.md
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/)
2 changes: 2 additions & 0 deletions examples/jsonpath-expressions/jsonpath_expressions.metatags
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
4 changes: 4 additions & 0 deletions examples/jsonpath-expressions/jsonpath_expressions.out
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

0 comments on commit 73b17ba

Please sign in to comment.