I managed to get some data from Sweden statistics using the api website. The answers to this question solved most of my problems.
How to send a POST formatted JSON request to get GET JSON data from a URL in R in data.frame in a less detailed way?
But I still have two problems.
If I have umlaut characters in my json question (for example, "Å", "Ä", "..."), I get the answer "404" from the server.
I am trying to load data from this table:
Population 16 years (RAMS) by region, employment, age and gender. Year 2004 - 2015
(You can get a request for api on the website if you click Continue and then api for this table, but you must change the response format from px to json.)
This code works:
library(jsonlite)
library(httr)
bodytxt <- '{
"query": [
{
"code": "Region",
"selection": {
"filter": "vs:RegionKommun07",
"values": [
"0114",
"1280"
]
}
},
{
"code": "Alder",
"selection": {
"filter": "item",
"values": [
"16-19"
]
}
},
{
"code": "Tid",
"selection": {
"filter": "item",
"values": [
"2015"
]
}
}
],
"response": {
"format": "json"
}
}'
req <- POST("http://api.scb.se/OV0104/v1/doris/en/ssd/START/AM/AM0207/AM0207H/BefSyssAldKonK",
body = bodytxt, encode = "json")
stop_for_status(req)
json <- content(req, "text")
validate(json)
json <- substring(json, 2)
validate(json)
object <- fromJSON(json)
print(object)
But if I modify the request, so it includes a "...", it does not work. Example:
bodytxt <- '{
"query": [
{
"code": "Region",
"selection": {
"filter": "vs:RegionKommun07",
"values": [
"0114",
"1280"
]
}
},
{
"code": "Sysselsattning",
"selection": {
"filter": "item",
"values": [
"FÖRV"
]
}
},
{
"code": "Alder",
"selection": {
"filter": "item",
"values": [
"16-19"
]
}
},
{
"code": "Tid",
"selection": {
"filter": "item",
"values": [
"2015"
]
}
}
],
"response": {
"format": "json"
}
}'
Another problem is that, as I understand it, it should be possible to change the json request to a list and include the list in a call to the server, but I get a "404" -error. Example:
body_list <- fromJSON(bodytxt)
req <- POST("http://api.scb.se/OV0104/v1/doris/en/ssd/START/AM/AM0207/AM0207H/BefSyssAldKonK",
body = body_list, encode = "json")
What am I doing wrong?
Ps! I know that there is a great CRAN package called pxweb, which is very easy to use to load data from Sweden statistics. But I want to know that api and pxwed do not allow me to skip measurements in the request.
System: Windows 7, r script is saved in utf-8 encoding.