Created
November 2, 2023 03:08
-
-
Save ballerina-github-bot/3e0955ba31b4089284a4b08e7b0df507 to your computer and use it in GitHub Desktop.
Ballerina Playground
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
import ballerina/http; | |
import ballerina/io; | |
// Define an open record type to describe both the payload on the wire and the data in memory | |
type Country record { | |
string country; | |
int population; | |
string continent; | |
int cases; | |
int deaths; | |
// Open records allow fields other than those specified | |
}; | |
// Prints the top 10 countries having the highest case-fatality ratio. | |
public function main() returns error? { | |
http:Client diseaseEp = check new ("https://disease.sh/v3/"); | |
// Perform a GET request and convert the JSON payload to the type 'Country' | |
Country[] countries = check diseaseEp->get("covid-19/countries"); | |
// Process data declaratively with language-integrated queries | |
json summary = | |
// Destructure each record value with a binding pattern | |
from var {country, continent, population, cases, deaths} in countries | |
where population >= 100000 && deaths >= 100 | |
let decimal caseFatalityRatio = <decimal>deaths / <decimal>cases * 100 | |
order by caseFatalityRatio descending | |
limit 10 | |
// Create a JSON object for each filtered country | |
select {country, continent, population, caseFatalityRatio}; | |
io:println(summary); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment