
Ensure JSON-stat2 output format in PxWebApi v2 URLs
Source:R/ensure_json_stat2.R
ensure_json_stat2.RdEnsures that a PxWebApi v2 URL requests data in JSON-stat2 format by enforcing
outputFormat=json-stat2.
Details
The function behaves as follows:
If the URL already contains
outputFormat=json-stat2, it is returned unchanged.If the URL contains an
outputFormatparameter with another value (e.g.csv), only the value is replaced byjson-stat2, leaving the rest of the URL unchanged.If the URL does not contain any
outputFormatparameter,outputFormat=json-stat2is appended.
Matching of parameter names and values is case-insensitive, but the resulting
URL always uses the canonical camelCase form
outputFormat=json-stat2.
This function is intended for internal use to guarantee a consistent and standardized output format across different PxWebApi v2 providers.
Examples
url1 <- "https://api.no/data?lang=en"
url1
#> [1] "https://api.no/data?lang=en"
ensure_json_stat2(url1)
#> [1] "https://api.no/data?lang=en&outputFormat=json-stat2"
url2 <- paste0(url1, "&valueCodes[Tid]=top(2)")
url2
#> [1] "https://api.no/data?lang=en&valueCodes[Tid]=top(2)"
ensure_json_stat2(url2)
#> [1] "https://api.no/data?lang=en&valueCodes[Tid]=top(2)&outputFormat=json-stat2"
url3 <- paste0(url1, "&outputFormat=csv&valueCodes[Tid]=?")
url3
#> [1] "https://api.no/data?lang=en&outputFormat=csv&valueCodes[Tid]=?"
ensure_json_stat2(url3)
#> [1] "https://api.no/data?lang=en&outputFormat=json-stat2&valueCodes[Tid]=?"