1

I am trying to fetch some data from an publicly available API available here:

http://prognos.konj.se/PxWeb/pxweb/en/SenastePrognosen/SenastePrognosen__f06_rantorochvaxelkurser/F0604.px/table/tableViewLayout1/

with the documentation being here:

https://www.scb.se/api_en/

Selecting the options according to the picture below:

enter image description here

Then I get the following table which tells me how to get the data through API:

enter image description here

My try of getting the data in Python was:

import requests

url = "http://prognos.konj.se/PxWeb/api/v1/en/SenastePrognosen/f06_rantorochvaxelkurser/F0604.px"

querystring = {
  "query": [
    {
      "code": "variabel",
      "selection": {
        "filter": "item",
        "values": [
          "F0604Sek10y"
        ]
      }
    },
    {
      "code": "period",
      "selection": {
        "filter": "item",
        "values": [
          "453",
          "454",
          "455"
        ]
      }
    }
  ],
  "response": {
    "format": "px"
  }
}


response = requests.request("GET", url, params=querystring)

print(response.text)

Does not yield the correct data (I get some weird output though? Edit: I selected 3 values in the first picture, now I got many more values in the output, my guess is that I should use the Json query in the 2:nd picture somehow?). Edit 2: the result is a JSON of the different query options available, without the expected data

This should be basic, but I do not get the correct answer. So, how should I fetch the wanted data from the API provided above?

EDIT:

The Answer I got from this was:

{"title":"Interest rates","variables":[{"code":"variabel","text":"variable","values":["F0604Sek10y","F0604Euro10y","F0604Usd10y","F0604Sek5y","F0604Ssvx6m","F0604Ssvx3m","F0604Euro3m","F0604Usd3m","F0604Repo_u","F0604Repo","F0604Euros_u","F0604Usds_u","F0604Kix6ranta_u","F0604Estr_u"],"valueTexts":["10-year government bond yield, Sweden","10-year government bond yield, Germany","10-year government bond yield, USA","5-year government bond yield, Sweden","6-month treasury bill rate, Sweden","3-month treasury bill rate, Sweden","3-month treasury bill rate, Germany","3-month treasury bill rate, USA","Repo rate, Sweden, at period end","Repo rate, Sweden, monthly average","Policy rate, euro area, at period end","Policy rate, USA, at period-end","Policy rate, KIX6-weighted, at period-end","Overnight rate, euro area (Estr), at period-end"]},{"code":"enhet","text":"units","values":["PCT"],"valueTexts":["Per cent"]},{"code":"period","text":"period","values":["0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50","51","52","53","54","55","56","57","58","59","60","61","62","63","64","65","66","67","68","69","70","71","72","73","74","75","76","77","78","79","80","81","82","83","84","85","86","87","88","89","90","91","92","93","94","95","96","97","98","99","100","101","102","103","104","105","106","107","108","109","110","111","112","113","114","115","116","117","118","119","120","121","122","123","124","125","126","127","128","129","130","131","132","133","134","135","136","137","138","139","140","141","142","143","144","145","146","147","148","149","150","151","152","153","154","155","156","157","158","159","160","161","162","163","164","165","166","167","168","169","170","171","172","173","174","175","176","177","178","179","180","181","182","183","184","185","186","187","188","189","190","191","192","193","194","195","196","197","198","199","200","201","202","203","204","205","206","207","208","209","210","211","212","213","214","215","216","217","218","219","220","221","222","223","224","225","226","227","228","229","230","231","232","233","234","235","236","237","238","239","240","241","242","243","244","245","246","247","248","249","250","251","252","253","254","255","256","257","258","259","260","261","262","263","264","265","266","267","268","269","270","271","272","273","274","275","276","277","278","279","280","281","282","283","284","285","286","287","288","289","290","291","292","293","294","295","296","297","298","299","300","301","302","303","304","305","306","307","308","309","310","311","312","313","314","315","316","317","318","319","320","321","322","323","324","325","326","327","328","329","330","331","332","333","334","335","336","337","338","339","340","341","342","343","344","345","346","347","348","349","350","351","352","353","354","355","356","357","358","359","360","361","362","363","364","365","366","367","368","369","370","371","372","373","374","375","376","377","378","379","380","381","382","383","384","385","386","387","388","389","390","391","392","393","394","395","396","397","398","399","400","401","402","403","404","405","406","407","408","409","410","411","412","413","414","415","416","417","418","419","420","421","422","423","424","425","426","427","428","429","430","431","432","433","434","435","436","437","438","439","440","441","442","443","444","445","446","447","448","449","450","451","452","453","454","455"],"valueTexts":["1993M01","1993M02","1993M03","1993M04","1993M05","1993M06","1993M07","1993M08","1993M09","1993M10","1993M11","1993M12","1994M01","1994M02","1994M03","1994M04","1994M05","1994M06","1994M07","1994M08","1994M09","1994M10","1994M11","1994M12","1995M01","1995M02","1995M03","1995M04","1995M05","1995M06","1995M07","1995M08","1995M09","1995M10","1995M11","1995M12","1996M01","1996M02","1996M03","1996M04","1996M05","1996M06","1996M07","1996M08","1996M09","1996M10","1996M11","1996M12","1997M01","1997M02","1997M03","1997M04","1997M05","1997M06","1997M07","1997M08","1997M09","1997M10","1997M11","1997M12","1998M01","1998M02","1998M03","1998M04","1998M05","1998M06","1998M07","1998M08","1998M09","1998M10","1998M11","1998M12","1999M01","1999M02","1999M03","1999M04","1999M05","1999M06","1999M07","1999M08","1999M09","1999M10","1999M11","1999M12","2000M01","2000M02","2000M03","2000M04","2000M05","2000M06","2000M07","2000M08","2000M09","2000M10","2000M11","2000M12","2001M01","2001M02","2001M03","2001M04","2001M05","2001M06","2001M07","2001M08","2001M09","2001M10","2001M11","2001M12","2002M01","2002M02","2002M03","2002M04","2002M05","2002M06","2002M07","2002M08","2002M09","2002M10","2002M11","2002M12","2003M01","2003M02","2003M03","2003M04","2003M05","2003M06","2003M07","2003M08","2003M09","2003M10","2003M11","2003M12","2004M01","2004M02","2004M03","2004M04","2004M05","2004M06","2004M07","2004M08","2004M09","2004M10","2004M11","2004M12","2005M01","2005M02","2005M03","2005M04","2005M05","2005M06","2005M07","2005M08","2005M09","2005M10","2005M11","2005M12","2006M01","2006M02","2006M03","2006M04","2006M05","2006M06","2006M07","2006M08","2006M09","2006M10","2006M11","2006M12","2007M01","2007M02","2007M03","2007M04","2007M05","2007M06","2007M07","2007M08","2007M09","2007M10","2007M11","2007M12","2008M01","2008M02","2008M03","2008M04","2008M05","2008M06","2008M07","2008M08","2008M09","2008M10","2008M11","2008M12","2009M01","2009M02","2009M03","2009M04","2009M05","2009M06","2009M07","2009M08","2009M09","2009M10","2009M11","2009M12","2010M01","2010M02","2010M03","2010M04","2010M05","2010M06","2010M07","2010M08","2010M09","2010M10","2010M11","2010M12","2011M01","2011M02","2011M03","2011M04","2011M05","2011M06","2011M07","2011M08","2011M09","2011M10","2011M11","2011M12","2012M01","2012M02","2012M03","2012M04","2012M05","2012M06","2012M07","2012M08","2012M09","2012M10","2012M11","2012M12","2013M01","2013M02","2013M03","2013M04","2013M05","2013M06","2013M07","2013M08","2013M09","2013M10","2013M11","2013M12","2014M01","2014M02","2014M03","2014M04","2014M05","2014M06","2014M07","2014M08","2014M09","2014M10","2014M11","2014M12","2015M01","2015M02","2015M03","2015M04","2015M05","2015M06","2015M07","2015M08","2015M09","2015M10","2015M11","2015M12","2016M01","2016M02","2016M03","2016M04","2016M05","2016M06","2016M07","2016M08","2016M09","2016M10","2016M11","2016M12","2017M01","2017M02","2017M03","2017M04","2017M05","2017M06","2017M07","2017M08","2017M09","2017M10","2017M11","2017M12","2018M01","2018M02","2018M03","2018M04","2018M05","2018M06","2018M07","2018M08","2018M09","2018M10","2018M11","2018M12","2019M01","2019M02","2019M03","2019M04","2019M05","2019M06","2019M07","2019M08","2019M09","2019M10","2019M11","2019M12","2020M01","2020M02","2020M03","2020M04","2020M05","2020M06","2020M07","2020M08","2020M09","2020M10","2020M11","2020M12","2021M01","2021M02","2021M03","2021M04","2021M05","2021M06","2021M07","2021M08","2021M09","2021M10","2021M11","2021M12","2022M01","2022M02","2022M03","2022M04","2022M05","2022M06","2022M07","2022M08","2022M09","2022M10","2022M11","2022M12","2023M01","2023M02","2023M03","2023M04","2023M05","2023M06","2023M07","2023M08","2023M09","2023M10","2023M11","2023M12","2024M01","2024M02","2024M03","2024M04","2024M05","2024M06","2024M07","2024M08","2024M09","2024M10","2024M11","2024M12","2025M01","2025M02","2025M03","2025M04","2025M05","2025M06","2025M07","2025M08","2025M09","2025M10","2025M11","2025M12","2026M01","2026M02","2026M03","2026M04","2026M05","2026M06","2026M07","2026M08","2026M09","2026M10","2026M11","2026M12","2027M01","2027M02","2027M03","2027M04","2027M05","2027M06","2027M07","2027M08","2027M09","2027M10","2027M11","2027M12","2028M01","2028M02","2028M03","2028M04","2028M05","2028M06","2028M07","2028M08","2028M09","2028M10","2028M11","2028M12","2029M01","2029M02","2029M03","2029M04","2029M05","2029M06","2029M07","2029M08","2029M09","2029M10","2029M11","2029M12","2030M01","2030M02","2030M03","2030M04","2030M05","2030M06","2030M07","2030M08","2030M09","2030M10","2030M11","2030M12"],"time":true}]}

EDIT 2: If you wish to extract some specific parts of the data, following @Gannon I tried:

x = json.loads(response.text)
print(x["value"])

This yields the error:

x = json.loads(response.text)
print(x["value"])
Traceback (most recent call last):

  File "<ipython-input-71-bb6ad4c1b9c5>", line 1, in <module>
    x = json.loads(response.text)

  File "C:\Users\MyProfile\anaconda3\lib\json\__init__.py", line 357, in loads
    return _default_decoder.decode(s)

  File "C:\Users\MyProfile\anaconda3\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())

  File "C:\Users\MyProfile\anaconda3\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None

JSONDecodeError: Expecting value

1 Answers1

0

Your query is failing. See below for the valid query, and you'll get the expected response (not a JSON issue at all).

I have received a valid response with 3 changes to your code:

  • using https
  • using response.post
  • changing params=... to json=querystring

Imports:

import requests
import json

Your code:

url = 'https://prognos.konj.se/PxWeb/api/v1/en/SenastePrognosen/f06_rantorochvaxelkurser/F0604.px'

querystring = {
  "query": [
    {
      "code": "variabel",
      "selection": {
        "filter": "item",
        "values": [
          "F0604Sek10y"
        ]
      }
    },
    {
      "code": "period",
      "selection": {
        "filter": "item",
        "values": [
          "453",
          "454",
          "455"
        ]
      }
    }
  ],
  "response": {
    "format": "json-stat2"
  }
}

response = requests.post(url, json=querystring)

x = json.loads(response.text)
print(x["value"])

Output:

[2.85, 2.85, 2.85]

With the GET request, you get a valid JSON containing the metadata needed to do a correct query, means query was missing (no POST data).

With the POST request, you get some raw data containing the following:

CHARSET="ANSI";
AXIS-VERSION="2010";
CODEPAGE="iso-8859-15";
LANGUAGE="sv";
LANGUAGES="sv","en";
DECIMALS=4;
SHOWDECIMALS=2;
MATRIX="F0604";
COPYRIGHT=NO;
...
CODES[en]("units")="PCT";
LAST-UPDATED[en]="20210929 09:15";
UNITS[en]="na";
CONTACT[en]="e-mail: statistik@konj.se";
SOURCE[en]="Sveriges Riksbank, ECB, Federal Reserve, Macrobond and National Institute of Economic Research";
DATASYMBOL2[en]="NA";
LINK[en]="http://www.konj.se/prognosdokumentationpx";
DATA=
2.8500 2.8500 2.8500 
;

You are searching for the results at the end of the response:

DATA=
2.8500 2.8500 2.8500 

By the way, if you wish to receive the result in a different format, just change the JSON query response format to "JSON",

...
"response": {
"format": "json-stat2"

}

Return options are, as per the documentation:

The response object is optional. If no response object is specified, a px file 
formatted result will be returned to the client.

Supported formats are:

• px
• csv
• json
• xlsx
• json-stat
• json-stat2
• sdmx

The format json-stat will return the response as JSON-stat version 1.2 and 
the format json-stat2 will return the response as JSON-stat 2.0.
Gannon
  • 68
  • 6
  • Hi MathStat2718, I wrote my Answer prior to your post edit... and as esqew sais : what is wrong with the JSON data ? Not what you expected ? – Gannon Oct 26 '21 at 20:04
  • Thank you very much @Gannon. In the first picture, I selected 3 values and I would like to get the data for those 3 values. But instead, I got a whole lot more of data? – MathStat2718 Oct 26 '21 at 20:07
  • @MathStat2718, you are correct, the response in not what you could expect. We are just receiving the list of all the parameters that can be queried on the database (some metadata), but no data. That probably means that the query format is wrong, or the url is wrong, or more steps are required. – Gannon Oct 26 '21 at 21:39
  • @MathStat2718, ok, checked the API, and as expected you just need to do a POST request, changing the line to `response = requests.post(url, json=querystring)` – Gannon Oct 26 '21 at 21:49
  • Thanks @gannon for the excellent answer. I would accept the answer but unfortunately the question has been closed (for beeing badly written, sorry for this). In the answer, I would like to extract the data values only. Do you know how to do this? My guess was something of the form: response["Data"]. – MathStat2718 Oct 28 '21 at 18:56
  • @MathStat2718, happy to help you out. My answer has been updated, check the code. Your question was respecting the Minimal/Complete/Reproducible, but probably not understood by the person that closed the question. The question gave all the info to find the problem. Initially I did miss the GET issue and did spend some time in documentation to figure out in the end that the answer to the problem was in one of the screenshots. To read the data, you first have to choose the return type. Maybe go for the json-stat2 format, as in my edited answer. – Gannon Oct 28 '21 at 19:36
  • Very elegant, I tried what you posted with the following error occuring (see last edit, EDIT 2). – MathStat2718 Oct 28 '21 at 19:58
  • Check the "format": "json-stat2" in place of px – Gannon Oct 28 '21 at 20:11
  • You are a legend! @Gannon – MathStat2718 Oct 28 '21 at 20:16