Skip to content

Instantly share code, notes, and snippets.

@ballerina-github-bot
Created October 5, 2023 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ballerina-github-bot/0578c1a7c58baef6ff1dd126bc3babb3 to your computer and use it in GitHub Desktop.
Save ballerina-github-bot/0578c1a7c58baef6ff1dd126bc3babb3 to your computer and use it in GitHub Desktop.
Ballerina Playground
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