Complex Datasets
Query complex data structures.
Click one of the examples below to learn more and deploy it.
Build an API over a JSON file
- Overview
- Code
Sample usage:
/patients-json[?country=<string>&minYear=<int>&maxYear=<int>&code=<string>]
The following URL returns the patients born between 1990 and 1995 that were diagnosed with L53.3.
/patients-json?minYear=1990&maxYear=1995&code=L53.3
main(country: string = null, minYear: int = null, maxYear: int = null, code: string = null) =
let
patients = Json.InferAndRead(
"https://raw-tutorial.s3.eu-west-1.amazonaws.com/patients.json")
in
Collection.Filter(
patients,
p -> (Nullable.IsNull(country) or p.country == country)
and (Nullable.IsNull(minYear) or p.year_of_birth >= minYear)
and (Nullable.IsNull(maxYear) or p.year_of_birth <= maxYear)
and (Nullable.IsNull(code) or
Collection.Count(Collection.Filter(p.diagnosis, (d) -> d.code == code)) > 0))
// The following test will run if you press the [Run Code] button directly.
main(minYear = 1990, maxYear = 1995, code = "L53.3")
Build an API over an XML file
- Overview
- Code
Sample usage:
/tutorial/people-xml[?name=<person_name>]
For instance, to get the information about bob use:
/tutorial/people-xml?name=bob
main(name: string = null) =
let people = Xml.InferAndRead("https://raw-tutorial.s3.amazonaws.com/inference/people.xml")
in Collection.Filter(people.person, x -> Nullable.IsNull(name) or x.name == name)
// The following test will run if you press the [Run Code] button directly.
main("bob")
Querying web services
- Overview
- Code
This example shows how to make HTTP requests:
- How to use different HTTP methods like GET, PUT, POST, DELETE, etc;
- How to pass headers;
- How to pass query paramenters.
Using HTTP URLs directly
Functions that read data from external locations can receive a URL. For instance, this example does an HTTP GET request on the source data.
Json.InferAndRead("http://test-data.raw-labs.com/public/authors.json")
It is equivalent to the following example:
Json.InferAndRead(Http.Get("http://test-data.raw-labs.com/public/authors.json"))
That is because HTTP URLs are converted directly to "Http.Get" requests.
However, sometimes you need to configure the HTTP request: use POST instead of GET, or pass headers or parameters. The next section describes how.
Making HTTP requests
To change HTTP method, specify headers, or pass query parameters along with other options, use the functions in the Http
library.
The following example performs a POST request - using the Http.Post
function - and passes a string as the HTTP POST body - using the optional argument bodyString
:
String.Read(
Http.Post(
"http://somewhere/api/clients"
bodyString = """{"name": "john", "query": "account"}"""
)
)
There are functions in the Http
library for each HTTP method: e.g. Http.Get
for GET requests, Http.Post
for POST requests, Http.Put
for PUT requests, etc.
Please refer to the documentation for additional details.
These functions accept the same arguments to pass headers, query parameters and other properties of the HTTP request.
Sample usage
The following example performs an HTTP request to wikidata with a SPARQL query listing cat entries.
It passes query parameters (args
) containing the query and uses HTTP headers (headers
) to set the output format as CSV:
main() =
let query = "SELECT ?item ?birthdate ?itemLabel \n" +
"WHERE {\n" +
" ?item wdt:P31 wd:Q146. # Must be of a cat \n" +
" ?item wdt:P569 ?birthdate . # P569 : birthdate\n" +
" SERVICE wikibase:label { bd:serviceParam wikibase:language \"[AUTO_LANGUAGE],en\". } \n" +
"}",
data = Csv.Read(
Http.Get(
"https://query.wikidata.org/bigdata/namespace/wdq/sparql",
args = [{"query", query}],
headers = [{"Accept", "text/csv"}]
),
type collection(record(item: string, birthdate: string, itemLabel: string)),
skip = 1
)
in data
main()
main() =
let query = "SELECT ?item ?birthdate ?itemLabel \n" +
"WHERE {\n" +
" ?item wdt:P31 wd:Q146. # Must be of a cat \n" +
" ?item wdt:P569 ?birthdate . # P569 : birthdate\n" +
" SERVICE wikibase:label { bd:serviceParam wikibase:language \"[AUTO_LANGUAGE],en\". } \n" +
"}",
data = Csv.Read(
Http.Get(
"https://query.wikidata.org/bigdata/namespace/wdq/sparql",
args = [{"query", query}],
headers = [{"Accept", "text/csv"}]
),
type collection(record(item: string, birthdate: string, itemLabel: string)),
skip = 1
)
in data
// The following test will run if you press the [Run Code] button directly.
main()