This question already has answers here:
HTTP requests and JSON parsing in Python [duplicate]
(8 answers)
Closed 3 years ago.
I have a REST Api that only renders this data
data: "xxxxxxxxxxxxxxx"
I need to store this XXXXXXXXXX value in some variable using python, but I could only code and reach to the part where I get this value data:"xxxxxxxxxxxxxx"
my code is as follows
r = requests.get('url', headers=headers, verify=False)
logger.info(r)
which gives the output data: "xxxxxxxxxxxxxxx"
How can i fetch only XXXXXXXXXXXX from this json output ?
As simple as this could work for you.
r = requests.get('url', headers=headers, verify=False).json()
print(r['data'])
Related
This question already has an answer here:
How to extract values from a Python request
(1 answer)
Closed 6 months ago.
How do I get a specific element from requests response.text
{"status":"success","data":{"id":30815925}}
im need to get ID from that output
You can import the json package and simply do
import json
id = json.loads(response.text)["data"]["id"]
This will convert response.text from a string to a python dictionary, and the id will be inside the variable id
This question already has answers here:
Loading JSON object in Python using urllib.request and json modules
(3 answers)
Closed 11 months ago.
URL = "API URL"
response = urllib.request.urlopen(URL)
standards = response.read() #returning type <bytes>
standards = standards.decode('utf-8') #converting type <str>
I actually want to read through the data and extract the values of "referenceNumber" & "title" from the data given below, I have 755 records of same structure. I want to extract the above given fields for each record.
[{"larsCode":0,"referenceNumber":"ST0870","title":"Business support assistant","status":"Withdrawn","url":"https://www.instituteforapprenticeships.org/api/apprenticeshipstandards/0","versionNumber":"0.0","change":"Withdrawn","changedDate":"2019-07-31T00:00:00","earliestStartDate":null,"latestStartDate":null,"latestEndDate":null,"overviewOfRole":"","level":2,"typicalDuration":0,"maxFunding":0,"route":"Business and administration","keywords":["Business","support","assistant","admin","office","office administration"],"jobRoles":[],"entryRequirements":"","assessmentPlanUrl":"","ssa1":"","ssa2":"","version":"0.0","standardInformation":"","occupationalSummary":"","knowledges":[],"behaviours":[],"skills":[],"options":[],"optionsUnstructuredTemplate":[]]
The server returns json encoded bytes. Python has a module for json.
You dont need to convert it to str in order to decode the json bytes.
Look at the python documentation for handling json objects.
Specifically json.loads for handling str or json.load for handling file like objects.
https://docs.python.org/3/library/json.html#json.loads
Just an fyi, your response is a list of objects so json.loads will return a list of dict.
This question already has answers here:
Are HTTP headers case-sensitive?
(7 answers)
Closed 2 years ago.
I want to send the same header parameter twice with python requests. is it possible?
When I try to do the following request, the Requests lib from python ignores one of the passed headers even sending it with different cases:
Example:
import requests
url = "http://www.example.com"
headers = {"test":"test1",
"Test":"test2"}
req = requests.get(url, headers=headers)
print(req.request.headers)
HTTP header names are not case-sensitive, so requests handles this correctly. That the keys in the dictionary are case-insensitive is also sometimes mentioned in requests' docs, like Session.headers, Response.headers
requests stores the request headers in a dict, which means every header can only appear once (+ not case-sensitive). So without making changes to the requests library itself it won't be possible to send multiple headers with the same name.
This question already has answers here:
Why can't Python parse this JSON data? [closed]
(3 answers)
Closed 4 years ago.
[{"sku":43900,"name":"Batteries (4-Pack)","type":"HardGood","category": [{"id":"pc5","name":"Housewares"}]]
this is in json file.
i want to load this and make a dictionary in Python from this input.i tired to load but confused how to create dictionary from this input.
import json
data = []
json_file=open ('prod.json')
json_str=json_file.read()
json_data=json.loads(json_str)
for items in json_data:
print(items['name'])
import json
s = '[{"sku":43900,"name":"Batteries (4-Pack)","type":"HardGood","category": [{"id":"pc5","name":"Housewares"}]}]'
list_of_dicts = json.loads(s)
Your json was invalid; I added a } in the right place.
This question already has answers here:
Let JSON object accept bytes or let urlopen output strings
(12 answers)
Closed 6 years ago.
I'm using flask in a web application that uses service api generates JSON response. The following part of the function works fine and returns JSON text output:
def get_weather(query = 'london'):
api_url = "http://api.openweathermap.org/data/2.5/weather?q={}&units=metric&appid=XXXXX****2a6eaf86760c"
query = urllib.request.quote(query)
url = api_url.format(query)
response = urllib.request.urlopen(url)
data = response.read()
return data
The output returned is:
{"coord":{"lon":-0.13,"lat":51.51},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04d"}],"base":"cmc stations","main":{"temp":12.95,"pressure":1030,"humidity":68,"temp_min":12.95,"temp_max":12.95,"sea_level":1039.93,"grnd_level":1030},"wind":{"speed":5.11,"deg":279.006},"clouds":{"all":76},"dt":1462290955,"sys":{"message":0.0048,"country":"GB","sunrise":1462249610,"sunset":1462303729},"id":2643743,"name":"London","cod":200}
This mean that data is a string, does not it?
However, commenting the return data and then adding the following two lines:
jsonData = json.loads(data)
return jsonData
generates the following error:
TypeError: the JSON object must be str, not 'bytes'
What's wrong? data, the JSON object, previously returned as a string! I need to know where is the mistake?
The data returned by request library is a binary string while json.loads accepts strings so you need to convert the data (decode) to a string using the encoding that your request returns (which is usually ok to assume that it is UTF-8).
You should be able to just change your code to this:
return json.loads(data.decode("utf-8"))
PS: Storing the variable right before returning it is redundant so I simplified things