Download data from Statistics Sweden. Swedish characters in request

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")


# JSON starts with an invalid character:
validate(json)
json <- substring(json, 2)
validate(json)

# Now we can parse
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.

+4
1

fromJSON():

library(httr)
library(jsonlite)

:

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"
  }
}'

fromJSON():

query <- jsonlite::fromJSON(bodytxt,
                            simplifyVector=FALSE,
                            simplifyDataFrame=FALSE)

, ( verbose() , ):

URL <- "http://api.scb.se/OV0104/v1/doris/en/ssd/START/AM/AM0207/AM0207H/BefSyssAldKonK"
req <- POST(URL, body=query, encode="json", verbose())

:

content(req, simplifyDataFrame=TRUE)

## Warning: JSON string contains (illegal) UTF8 byte-order-mark!

## $columns
##             code                        text type
## 1         Region                      region    d
## 2 Sysselsattning           employment status    d
## 3          Alder                         age    d
## 4            Tid                        year    t
## 5       AM0207F2 Population 16+ years (RAMS)    c
## 
## $comments
## list()
## 
## $data
##                       key values
## 1 0114, FÖRV, 16-19, 2015    379
## 2 1280, FÖRV, 16-19, 2015   1443

- , API ( Microsoft, , ). jsonlite ( content(), ) .

+3

Source: https://habr.com/ru/post/1670271/


All Articles