Learn R Programming

PxWebApiData (version 1.9.0)

ensure_json_stat2: Ensure JSON-stat2 output format in PxWebApi v2 URLs

Description

Ensures that a PxWebApi v2 URL requests data in JSON-stat2 format by enforcing outputFormat=json-stat2.

Usage

ensure_json_stat2(url)

Value

A character string with outputFormat=json-stat2 enforced.

Arguments

url

A character string of length one giving a PxWebApi v2 data URL.

Details

The function behaves as follows:

  • If the URL already contains outputFormat=json-stat2, it is returned unchanged.

  • If the URL contains an outputFormat parameter with another value (e.g. csv), only the value is replaced by json-stat2, leaving the rest of the URL unchanged.

  • If the URL does not contain any outputFormat parameter, outputFormat=json-stat2 is 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

Run this code
url1 <- "https://api.no/data?lang=en"
url1
ensure_json_stat2(url1)

url2 <- paste0(url1, "&valueCodes[Tid]=top(2)")
url2
ensure_json_stat2(url2)

url3 <- paste0(url1, "&outputFormat=csv&valueCodes[Tid]=?")
url3
ensure_json_stat2(url3)

Run the code above in your browser using DataLab