Difficulties using Python request (POST) + API - python
Im trying to use a simple API with python. I get the data with the code below but, but I can't seem to parse it. When i print the type of variable "c" it says "unicode". I want a Json object or dictionary so I can use the information.
I have I tried various ways to solve this but I'm not sure if the output from the API (below) is actually Json or why it doesn't work properly.
import requests
import json
import urllib
test1 ={
"query": [
{
"code": "SNI2007",
"selection": {
"filter": "item",
"values": [
"47.4+47.54"
]
}
},
{
"code": "ContentsCode",
"selection": {
"filter": "item",
"values": [
"HA0101A9",
"HA0101B4"
]
}
},
{
"code": "Tid",
"selection": {
"filter": "item",
"values": [
"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"
]
}
}
],
"response": {
"format": "json"
}
}
response = requests.post("http://api.scb.se/OV0104/v1/doris/sv/ssd/START/HA/HA0101/HA0101B/Detoms07", json = test1)
dat = response.content
b = json.dumps(dat)
c = json.loads(b)
print type(b)
This is what I get if I print the "response.content" variable.
{"columns":[{"code":"SNI2007","text":"näringsgren SNI 2007","type":"d"},{"code":"Tid","text":"månad","type":"t"},{"code":"HA0101A9","text":"Löpande priser","type":"c"},{"code":"HA0101B4","text":"Fasta priser","type":"c"}],"comments":[],"data":[{"key":["47.4+47.54","2010M01"],"values":["90.3","45.0"]},{"key":["47.4+47.54","2010M02"],"values":["80.9","40.3"]},{"key":["47.4+47.54","2010M03"],"values":["91.3","45.7"]},{"key":["47.4+47.54","2010M04"],"values":["83.9","43.5"]},{"key":["47.4+47.54","2010M05"],"values":["87.4","45.7"]},{"key":["47.4+47.54","2010M06"],"values":["97.6","52.6"]},{"key":["47.4+47.54","2010M07"],"values":["99.5","54.2"]},{"key":["47.4+47.54","2010M08"],"values":["105.2","57.3"]},{"key":["47.4+47.54","2010M09"],"values":["108.9","60.4"]},{"key":["47.4+47.54","2010M10"],"values":["107.9","60.7"]},{"key":["47.4+47.54","2010M11"],"values":["107.9","61.3"]},{"key":["47.4+47.54","2010M12"],"values":["181.9","106.1"]},{"key":["47.4+47.54","2011M01"],"values":["95.3","55.9"]},{"key":["47.4+47.54","2011M02"],"values":["80.1","47.3"]},{"key":["47.4+47.54","2011M03"],"values":["88.8","53.5"]},{"key":["47.4+47.54","2011M04"],"values":["79.4","48.5"]},{"key":["47.4+47.54","2011M05"],"values":["85.9","53.0"]},{"key":["47.4+47.54","2011M06"],"values":["90.2","57.3"]},{"key":["47.4+47.54","2011M07"],"values":["95.5","61.1"]},{"key":["47.4+47.54","2011M08"],"values":["97.1","62.3"]},{"key":["47.4+47.54","2011M09"],"values":["96.3","62.4"]},{"key":["47.4+47.54","2011M10"],"values":["97.0","63.6"]},{"key":["47.4+47.54","2011M11"],"values":["104.5","69.2"]},{"key":["47.4+47.54","2011M12"],"values":["171.4","113.9"]},{"key":["47.4+47.54","2012M01"],"values":["93.7","62.8"]},{"key":["47.4+47.54","2012M02"],"values":["78.3","53.1"]},{"key":["47.4+47.54","2012M03"],"values":["87.2","60.1"]},{"key":["47.4+47.54","2012M04"],"values":["82.7","57.4"]},{"key":["47.4+47.54","2012M05"],"values":["81.1","56.8"]},{"key":["47.4+47.54","2012M06"],"values":["92.8","66.3"]},{"key":["47.4+47.54","2012M07"],"values":["88.4","64.0"]},{"key":["47.4+47.54","2012M08"],"values":["92.7","68.0"]},{"key":["47.4+47.54","2012M09"],"values":["96.1","71.5"]},{"key":["47.4+47.54","2012M10"],"values":["92.4","69.7"]},{"key":["47.4+47.54","2012M11"],"values":["99.2","75.9"]},{"key":["47.4+47.54","2012M12"],"values":["147.5","115.5"]},{"key":["47.4+47.54","2013M01"],"values":["89.6","70.6"]},{"key":["47.4+47.54","2013M02"],"values":["75.5","59.9"]},{"key":["47.4+47.54","2013M03"],"values":["79.5","63.7"]},{"key":["47.4+47.54","2013M04"],"values":["76.2","62.0"]},{"key":["47.4+47.54","2013M05"],"values":["79.0","65.0"]},{"key":["47.4+47.54","2013M06"],"values":["84.6","70.5"]},{"key":["47.4+47.54","2013M07"],"values":["85.7","73.0"]},{"key":["47.4+47.54","2013M08"],"values":["91.6","77.8"]},{"key":["47.4+47.54","2013M09"],"values":["90.6","77.4"]},{"key":["47.4+47.54","2013M10"],"values":["93.0","79.8"]},{"key":["47.4+47.54","2013M11"],"values":["97.4","84.3"]},{"key":["47.4+47.54","2013M12"],"values":["151.0","133.0"]},{"key":["47.4+47.54","2014M01"],"values":["92.3","81.6"]},{"key":["47.4+47.54","2014M02"],"values":["75.7","67.6"]},{"key":["47.4+47.54","2014M03"],"values":["82.3","74.5"]},{"key":["47.4+47.54","2014M04"],"values":["79.6","72.7"]},{"key":["47.4+47.54","2014M05"],"values":["80.3","73.9"]},{"key":["47.4+47.54","2014M06"],"values":["92.7","85.9"]},{"key":["47.4+47.54","2014M07"],"values":["88.0","82.7"]},{"key":["47.4+47.54","2014M08"],"values":["94.4","88.6"]},{"key":["47.4+47.54","2014M09"],"values":["100.2","95.3"]},{"key":["47.4+47.54","2014M10"],"values":["103.0","98.9"]},{"key":["47.4+47.54","2014M11"],"values":["104.4","100.0"]},{"key":["47.4+47.54","2014M12"],"values":["159.9","154.1"]},{"key":["47.4+47.54","2015M01"],"values":["95.9","93.3"]},{"key":["47.4+47.54","2015M02"],"values":["80.5","78.3"]},{"key":["47.4+47.54","2015M03"],"values":["90.4","88.5"]},{"key":["47.4+47.54","2015M04"],"values":["82.6","81.2"]},{"key":["47.4+47.54","2015M05"],"values":["85.9","84.4"]},{"key":["47.4+47.54","2015M06"],"values":["97.5","96.8"]},{"key":["47.4+47.54","2015M07"],"values":["95.1","95.0"]},{"key":["47.4+47.54","2015M08"],"values":["93.7","93.8"]},{"key":["47.4+47.54","2015M09"],"values":["98.4","99.4"]},{"key":["47.4+47.54","2015M10"],"values":["105.5","107.5"]},{"key":["47.4+47.54","2015M11"],"values":["114.6","116.9"]},{"key":["47.4+47.54","2015M12"],"values":["159.9","164.9"]},{"key":["47.4+47.54","2016M01"],"values":["91.4","95.8"]},{"key":["47.4+47.54","2016M02"],"values":["84.7","90.1"]},{"key":["47.4+47.54","2016M03"],"values":["89.6","96.2"]},{"key":["47.4+47.54","2016M04"],"values":["87.9","94.8"]},{"key":["47.4+47.54","2016M05"],"values":["84.6","92.1"]},{"key":["47.4+47.54","2016M06"],"values":["95.0","105.6"]},{"key":["47.4+47.54","2016M07"],"values":["93.0","104.3"]},{"key":["47.4+47.54","2016M08"],"values":["96.1","106.9"]},{"key":["47.4+47.54","2016M09"],"values":["98.2","110.5"]},{"key":["47.4+47.54","2016M10"],"values":["103.2","116.4"]},{"key":["47.4+47.54","2016M11"],"values":["116.6","132.3"]},{"key":["47.4+47.54","2016M12"],"values":["155.6","177.2"]},{"key":["47.4+47.54","2017M01"],"values":["94.7","108.3"]},{"key":["47.4+47.54","2017M02"],"values":["79.2","91.4"]},{"key":["47.4+47.54","2017M03"],"values":["88.8","102.8"]},{"key":["47.4+47.54","2017M04"],"values":["80.3","93.9"]},{"key":["47.4+47.54","2017M05"],"values":["82.9","97.4"]},{"key":["47.4+47.54","2017M06"],"values":["94.2","111.0"]},{"key":["47.4+47.54","2017M07"],"values":["88.3","103.4"]},{"key":["47.4+47.54","2017M08"],"values":["91.0","105.8"]},{"key":["47.4+47.54","2017M09"],"values":["92.6","107.9"]},{"key":["47.4+47.54","2017M10"],"values":["97.9","115.2"]},{"key":["47.4+47.54","2017M11"],"values":["121.2","142.7"]},{"key":["47.4+47.54","2017M12"],"values":["149.7","177.7"]},{"key":["47.4+47.54","2018M01"],"values":["98.1","116.3"]},{"key":["47.4+47.54","2018M02"],"values":["79.0","94.6"]},{"key":["47.4+47.54","2018M03"],"values":["93.0","112.9"]},{"key":["47.4+47.54","2018M04"],"values":["85.6","104.3"]}]}
There's two strategies you can use, response.json() will give you back a dict with all the JSON in key-value format using requests internal JSON parser, the other if you want to use the actual json library is to do json_data = json.loads(response.text) and allow the json library to parse it instead.
In general the requests JSON parser is probably enough for what you need.
Related
How to get the value of "authorization" from Json using python?
I have a Json data, where I want to get the value of "authorization" i.e "OToken DEDC1071B77800A146B6E8D2530E0429E76520C151B40CC3325D8B 6D9242CBA3A6BFA643E7E5596FBEBAE0F46A1FB1BCD099EBC1F59D CD82F390B6BC45FCE036F37F7F589BD687A691E1378F1FF432331C 62E7E641E857C8F8A405A4BFE2F01B1EB8F3C69817D45F5DDE9DEE 346ACABA1B7208DECA9E43CCE7AB3761553E23D9CB36A870C1819C 15C7C4B1CFE2802DFD05F651AA537AB81787.4145535F55415431" using python { "links":[ { "method":"GET", "rel":"self", "href":"https://www.sampleurl.com/request" }, { "headers":{ "authorization":"OToken DEDC1071B77800A146B6E8D2530E0429E76520C151B40CC3325D8B 6D9242CBA3A6BFA643E7E5596FBEBAE0F46A1FB1BCD099EBC1F59D CD82F390B6BC45FCE036F37F7F589BD687A691E1378F1FF432331C 62E7E641E857C8F8A405A4BFE2F01B1EB8F3C69817D45F5DDE9DEE 346ACABA1B7208DECA9E43CCE7AB3761553E23D9CB36A870C1819C 15C7C4B1CFE2802DFD05F651AA537AB81787.4145535F55415431" }, "valid_date":"2020-08-17T15:49:00+0530", "method":"POST", "rel":"redirect", "href":"https://www.billdesk.com/pgi/MerchantPayment/", "parameters":{ "mercid":"BDMERCID", "bdorderid":"OAFC19XTFD8TSP" } } ] }
import json response = YOUR_JSON_STRING data = json.loads(response) authorization = data['headers']['authorization']
Python requests accessing index of response object
I'm new to python and trying to practice aggregating data from an api call i have this script r = requests.get('https://jsonplaceholder.typicode.com/users') print r.text which returns an array of objects in this format [{ "id": 1, "name": "Leanne Graham", "username": "Bret", "email": "Sincere#april.biz", "address": { "street": "Kulas Light", "suite": "Apt. 556", "city": "Gwenborough", "zipcode": "92998-3874", "geo": { "lat": "-37.3159", "lng": "81.1496" } }] I've been playing around and tried this to see if I can access the first object print r.text[0] And it didn't work. So how do I do this with python
You need to parse the JSON text: import json array = json.loads(r) print array[0]
request.text returns Http response body. so If you want to get first propery of json, you should convert string to json object. This works result = r.text print(type) # prints str import json result = json.loads(result) print(result[0]) # (...)
Python: bitly request
I'm trying to do a basic Bitly shortening URL call. However, I cannot seem to either push the json correctly, or deal with the json response correctly... I omitted some obvious variables for brevity and obfuscated some real values for security purposes. import requests import json bitly_header = {'Authorization':'Bearer some_long_secret_character_string_here', 'Content-Type':'application/json'} bitly_data = { "long_url": ""+long_url+"", "group_guid": ""+bitly_guid+"" } short_link_resp =requests.post(bitly_endpoint,data=bitly_data,headers=bitly_header) short_link_json = short_link_resp.json() short_link = short_link_json["link"] Errors is "Key error: 'link' The json I get from Postman is: { "created_at": "1970-01-01T00:00:00+0000", "id": "bit.ly/2MjdrrG", "link": "bit.ly/2MjdrrG", "custom_bitlinks": [], "long_url": "google.com/", "archived": false, "tags": [], "deeplinks": [], "references": { "group": "https://api-ssl.bitly.com/v4/groups/Bi7i8IbM1x9" } }
try replace data with json: short_link_resp =requests.post(bitly_endpoint, json=bitly_data, headers=bitly_header) see the doc ref.
string indices must be integers error with json
I am trying to parse out following json using pythong: { "document_tone":{ "tone_categories":[ { "tones":[ { "score":0.044115, "tone_id":"anger", "tone_name":"Anger" }, { "score":0.005631, "tone_id":"disgust", "tone_name":"Disgust" }, { "score":0.013157, "tone_id":"fear", "tone_name":"Fear" }, { "score":1.0, "tone_id":"joy", "tone_name":"Joy" }, { "score":0.058781, "tone_id":"sadness", "tone_name":"Sadness" } ], "category_id":"emotion_tone", "category_name":"Emotion Tone" }, { "tones":[ { "score":0.0, "tone_id":"analytical", "tone_name":"Analytical" }, { "score":0.0, "tone_id":"confident", "tone_name":"Confident" }, { "score":0.0, "tone_id":"tentative", "tone_name":"Tentative" } ], "category_id":"language_tone", "category_name":"Language Tone" }, { "tones":[ { "score":0.0, "tone_id":"openness_big5", "tone_name":"Openness" }, { "score":0.571, "tone_id":"conscientiousness_big5", "tone_name":"Conscientiousness" }, { "score":0.936, "tone_id":"extraversion_big5", "tone_name":"Extraversion" }, { "score":0.978, "tone_id":"agreeableness_big5", "tone_name":"Agreeableness" }, { "score":0.975, "tone_id":"emotional_range_big5", "tone_name":"Emotional Range" } ], "category_id":"social_tone", "category_name":"Social Tone" } ] } } and here is the code that I am trying to use following code to get tone name and score from the json: import json from watson_developer_cloud import ToneAnalyzerV3Beta import urllib.request import codecs reader = codecs.getreader("utf-8") tone_analyzer = ToneAnalyzerV3Beta( url='https://gateway.watsonplatform.net/tone-analyzer/api', username='<username>', password='<password>', version='2016-02-11') data=json.dumps(tone_analyzer.tone(text='I am very happy'), indent=2) print (data) for cat in data['document_tone']['tone_categories']: print('Category:', cat['category_name']) for tone in cat['tones']: print('-', tone['tone_name']) but keep running into the error string indices must be integers. I tried to ask the same question in one of my earlier post but with this post I am providing some more details. I would really appreciate any inputs with this. Thank You
tone_analyzer.tone(text='I am very happy') returns a dictionary, there is no need to use json to modify the data in any way, just do X = tone_analyzer.tone(text='I am very happy') Note that you have already recieved this exact answer on your previous question.
Script to send json using POST/REST and requests in python doesn't work
I'm trying to send a json in POST method using REST, but i got error: "Could not parse JSON data: Expecting value: line 1 column 1 (char 0)", "status": 500, "type": "ValueError", "request": {"client_addr": "127.0.0.1", "user_agent": "python-requests/2.3.0 CPython/3.4.2 Linux/3.16.0-41-generic", "method": "POST", "path": "/api/adley/doc"}} ) I'm trying to fix, use a json.dumps or json.loads, but nothing seems to work. I need to send one key and two values. Here is the base: { metadata: { idx_exp: false, idx_exp_time: 0, file_ext: false, password: "", description: "base de teste", idx_exp_url: "", model: { item: "Text" }, dt_base: "29/06/2015 14:47:10", name: "adley", id_base: 5, file_ext_time: 0, }, content: [ { field: { required: false, multivalued: false, alias: "Texto", datatype: "Text", name: "item", indices: [ "Ordenado" ], description: "placeholder" } } ] } My to send a post script: import requests, json url = "http://127.0.0.1/api/adley/doc" json_data = {'value':{'item':'test'}} response = requests.post(url, params=json_data) print(response.text) I can't see what's wrong with my script
try passing the data parameter instead of the params parameter to the post method. requests wants to url encode params where you want the json data. So something like this: requests.post(url, data=json.dumps(json_data))