How to get json elements from http response string in python - python

I am new in json parsing from http api in python.Currently i have parsed http content as string in python which have json object array my code is given bellow
import json
from urllib.request import urlopen
apilink=urlopen("api link")
data=json.loads(apilink.read().decode())
print(data)
and my current output is
{'Message': 'Success', 'Data': '[{"Did":"c055c3d2f3314725b69965e6c55adb5b","InsertedDate":"2017-08-02 7:27:11 AM","UpdatedDate":"2017-08-02 9:33:16 AM","CreatedBy":"1","UpdatedBy":"1","Name":"Hello World","ModuleName":"Rpt_Hello_World","ApplicationName":"Asset Inventory","Published":"true","UserId":"1","PostProcessor":""}]', 'Status': 'Success'}
but i want to extract only attribute 'data' that is json array
'Data': '[{"Did":"c055c3d2f3314725b69965e6c55adb5b","InsertedDate":"2017-08-02 7:27:11 AM","UpdatedDate":"2017-08-02 9:33:16 AM","CreatedBy":"1","UpdatedBy":"1","Name":"Hello World","ModuleName":"Rpt_Hello_World","ApplicationName":"Asset Inventory","Published":"true","UserId":"1","PostProcessor":""}]'
desirble part is
[{"Did":"c055c3d2f3314725b69965e6c55adb5b","InsertedDate":"2017-08-02 7:27:11 AM","UpdatedDate":"2017-08-02 9:33:16 AM","CreatedBy":"1","UpdatedBy":"1","Name":"Hello World","ModuleName":"Rpt_Hello_World","ApplicationName":"Asset Inventory","Published":"true","UserId":"1","PostProcessor":""}]
kindly help me to solve this.
thank you

data is a dictionary. Use dict indexing. You need the value associated with Data:
In [876]: data['Data']
Out[876]: '[{"Did":"c055c3d2f3314725b69965e6c55adb5b","InsertedDate":"2017-08-02 7:27:11 AM","UpdatedDate":"2017-08-02 9:33:16 AM","CreatedBy":"1","UpdatedBy":"1","Name":"Hello World","ModuleName":"Rpt_Hello_World","ApplicationName":"Asset Inventory","Published":"true","UserId":"1","PostProcessor":""}]'
This is a string. You can use json.loads one more time.
In [877]: json.loads(data['Data'])
Out[877]:
[{'ApplicationName': 'Asset Inventory',
'CreatedBy': '1',
'Did': 'c055c3d2f3314725b69965e6c55adb5b',
'InsertedDate': '2017-08-02 7:27:11 AM',
'ModuleName': 'Rpt_Hello_World',
'Name': 'Hello World',
'PostProcessor': '',
'Published': 'true',
'UpdatedBy': '1',
'UpdatedDate': '2017-08-02 9:33:16 AM',
'UserId': '1'}]

thats simple
format: jsonName[attributename]
data['Data']

Related

How to get a nested response data from json python

I call an API and this my JSON response:
{'data': [{
'id': 'd-1225959',
'startTime': '2022-12-30T00:00:00.000Z',
'endTime': '2022-12-30T23:59:00.000Z',
'checkedInAt': None,
'checkedOutAt': None,
'status': 'PENDING',
'space': {
'id': 'd-4063963',
'name': '082',
'type': 'DESK',
'createdAt': '2021-07-06T11:48:57.000Z',
'updatedAt': '2021-07-06T11:48:57.000Z',
'isAvailable': False,
'assignedTo': None,
'locationId': '133778',
'floorId': '41681',
'floorName': 'Car Park',
'neighborhoodId': '92267',
'neighborhoodName': 'NEI1'}}
I'm struggling to get the 'space' 'id' and 'name' extracted out if I do a nested python loop like so it only returns the headers like 'id' and 'name' not the values held within.
for order in response['data']:
print(order['id'])
print(order['startTime'])
print(order['endTime'])
print(order['checkedInAt'])
print(order['checkedOutAt'])
print(order['status'])
print(order['space'])
for doc in response['space']:
print(doc['id'], doc['name'])
Any help with this would be much appreciated!
for doc in response['space']: will iterate over the keys in response['space'] dict, i.e. doc will be str.
You want to do doc = response['space'] instead and then print(doc['id']). or directly print(response['space']['id']).
Note, you may want to use dict.get() method to avoid KeyError.
# if response dict has no 'space' key, return empty dict.
# if no 'id' key - return None
space_id = response.get('space', {}).get('id')

extract a link from a web page with python

I'm writing a code in which it sends the site's link in a chat (I know how to do this part), I make the request, but in this request it returns other things along with the link, how do I get only the link?
link = requests.get(f"https://sugoi-api.herokuapp.com/episode/{Episodio}/{AnimeN}")
resultado = link.json()
this is the result:
{'status': 200, 'info': {'name': 'Naruto classico', 'slug': 'naruto-classico', 'fc': 'N', 'epi': '12'}, 'cdn': [{'name': 'Superanimes', 'url': 'https://cdn.superanimes.tv/', 'links': ['https://cdn.superanimes.tv/010/animes/n/naruto-classico-dublado/12.mp4', 'https://cdn.superanimes.tv/010/animes/n/naruto-classico-legendado/12.mp4%27]%7D, {'name': 'Serverotaku', 'url': 'https://cdn.serverotaku01.co/', 'links': ['https://cdn.serverotaku01.co/010/animes/n/naruto-classico-dublado/12.mp4', 'https://cdn.serverotaku01.co/010/animes/n/naruto-classico-legendado/12.mp4%27]%7D, {'name': 'Servertv', 'url': 'https://servertv001.com/', 'links': ['https://servertv001.com/animes/n/naruto-classico-dublado/12.mp4', 'https://servertv001.com/animes/n/naruto-classico-legendado/12.mp4%27]%7D]%7D
if someone knows how to get only the result link it would help me a lot
One simple way to extract URL from any data (general) is mentioned below. First, Convert the json output you got into a string and then use regular expression.
str = json.dumps({'status': 200, 'info':........})
import re
re.findall("(?P<url>https?://[^\s]+)", str)

passing data in request using python

I have a scenario where I want to pass the data from a dict to a endpoint in a for loop.
here is what I have tried :
data3= [{'spcg': [{'a_id': 234, 'b_id': 1122, 'cls': [{'address': '{"fgt":"Cc","Ik":"01","IV_Y":"CP1","IID":"0054","IY":1,"SL":"https://ap.com","OC":""}', 'cl_id': 456103, 'type': 'SC', 'name': 'SFTPC', 'srt': 'sdp%', 'oner': 'ar'}], 'adata': False}], 'e_id': 1122}, {'spcg': [{'a_id': 456, 'b_id': 1111, 'cls': [{'address': '{"fgt":"Cc","Ik":"01","IV_Y":"CP1","IID":"0054","IY":1,"SL":"https://ap.com","OC":""}', 'cl_id': 4545673, 'type': 'SG', 'name': 'SMC', 'srt': 'sdfe', 'oner': 'agtr'}], 'adata': False}], 'e_id': 1111}]
for key in data3:
temp=(key['spcg'])
ab=requests.post('https://app/v1/end', headers =headers_active_spc, data=temp)
print(temp)
Error I am receiveing :
ValueError: too many values to unpack (expected 2)
in
ab=requests.post('https://app/v1/end', headers =headers_active_spc, data=temp)
How can I pass all the spcg values ad body in POST request one by one as I tried in for loop.
try sending post request with JSON format data=json.dumps(temp)
where json can be imported as import json

Accessing keys/values in a paginated/nested dictionary

I know that somewhat related questions have been asked here: Accessing key, value in a nested dictionary and here: python accessing elements in a dictionary inside dictionary among other places but I can't quite seem to apply the answers' methodology to my issue.
I'm getting a KeyError trying to access the keys within response_dict, which I know is due to it being nested/paginated and me going about this the wrong way. Can anybody help and/or point me in the right direction?
import requests
import json
URL = "https://api.constantcontact.com/v2/contacts?status=ALL&limit=1&api_key=<redacted>&access_token=<redacted>"
#make my request, store it in the requests object 'r'
r = requests.get(url = URL)
#status code to prove things are working
print (r.status_code)
#print what was retrieved from the API
print (r.text)
#visual aid
print ('---------------------------')
#decode json data to a dict
response_dict = json.loads(r.text)
#show how the API response looks now
print(response_dict)
#just for confirmation
print (type(response_dict))
print('-------------------------')
# HERE LIES THE ISSUE
print(response_dict['first_name'])
And my output:
200
{"meta":{"pagination":{}},"results":[{"id":"1329683950","status":"ACTIVE","fax":"","addresses":[{"id":"4e19e250-b5d9-11e8-9849-d4ae5275509e","line1":"222 Fake St.","line2":"","line3":"","city":"Kansas City","address_type":"BUSINESS","state_code":"","state":"OK","country_code":"ve","postal_code":"19512","sub_postal_code":""}],"notes":[],"confirmed":false,"lists":[{"id":"1733488365","status":"ACTIVE"}],"source":"Site Owner","email_addresses":[{"id":"1fe198a0-b5d5-11e8-92c1-d4ae526edd6c","status":"ACTIVE","confirm_status":"NO_CONFIRMATION_REQUIRED","opt_in_source":"ACTION_BY_OWNER","opt_in_date":"2018-09-11T18:18:20.000Z","email_address":"rsmith#fake.com"}],"prefix_name":"","first_name":"Robert","middle_name":"","last_name":"Smith","job_title":"I.T.","company_name":"FBI","home_phone":"","work_phone":"5555555555","cell_phone":"","custom_fields":[],"created_date":"2018-09-11T15:12:40.000Z","modified_date":"2018-09-11T18:18:20.000Z","source_details":""}]}
---------------------------
{'meta': {'pagination': {}}, 'results': [{'id': '1329683950', 'status': 'ACTIVE', 'fax': '', 'addresses': [{'id': '4e19e250-b5d9-11e8-9849-d4ae5275509e', 'line1': '222 Fake St.', 'line2': '', 'line3': '', 'city': 'Kansas City', 'address_type': 'BUSINESS', 'state_code': '', 'state': 'OK', 'country_code': 've', 'postal_code': '19512', 'sub_postal_code': ''}], 'notes': [], 'confirmed': False, 'lists': [{'id': '1733488365', 'status': 'ACTIVE'}], 'source': 'Site Owner', 'email_addresses': [{'id': '1fe198a0-b5d5-11e8-92c1-d4ae526edd6c', 'status': 'ACTIVE', 'confirm_status': 'NO_CONFIRMATION_REQUIRED', 'opt_in_source': 'ACTION_BY_OWNER', 'opt_in_date': '2018-09-11T18:18:20.000Z', 'email_address': 'rsmith#fake.com'}], 'prefix_name': '', 'first_name': 'Robert', 'middle_name': '', 'last_name': 'Smith', 'job_title': 'I.T.', 'company_name': 'FBI', 'home_phone': '', 'work_phone': '5555555555', 'cell_phone': '', 'custom_fields': [], 'created_date': '2018-09-11T15:12:40.000Z', 'modified_date': '2018-09-11T18:18:20.000Z', 'source_details': ''}]}
<class 'dict'>
-------------------------
Traceback (most recent call last):
File "C:\Users\rkiek\Desktop\Python WIP\Chris2.py", line 20, in <module>
print(response_dict['first_name'])
KeyError: 'first_name'
first_name = response_dict["results"][0]["first_name"]
Even though I think this question would be better answered by yourself by reading some documentation, I will explain what is going on here. You see the dict-object of the man named "Robert" is within a list which is a value under the key "results". So, at first you need to access the value within results which is a python-list.
Then you can use a loop to iterate through each of the elements within the list, and treat each individual element as a regular dictionary object.
results = response_dict["results"]
results = response_dict.get("results", None)
# use any one of the two above, the first one will throw a KeyError if there is no key=="results" the other will return NULL
# this results is now a list according to the data you mentioned.
for item in results:
print(item.get("first_name", None)
# here you can loop through the list of dictionaries and treat each item as a normal dictionary

How do you pull text from a website into a dict?

I'm attempting to get the information from http://xkcd.com/info.0.json. Basically it looks like a simple python dictionary and that's what I'd like to convert it to. My current code is:
import urllib.request
with urllib.request.urlopen('http://xkcd.com/info.0.json') as response:
html = [response.read()]
print(html)
and that outputs
[b'{"month": "2", "num": 1647, "link": "", "year": "2016", "news": "", "safe_title": "Diacritics", "transcript": "", "alt": "Using diacritics correctly is not my fort\\u00c3\\u00a9.", "img": "http:\\/\\/imgs.xkcd.com\\/comics\\/diacritics.png", "title": "Diacritics", "day": "24"}']
You are receiving a JSON encoded response. You can parse that with the json.loads() function:
import json
import urllib.request
with urllib.request.urlopen('http://xkcd.com/info.0.json') as response:
data = json.loads(response.read().decode('utf8'))
>>> data
{'link': '', 'transcript': '', 'month': '2', 'year': '2016', 'alt': 'Using diacritics correctly is not my forté.', 'num': 1647, 'img': 'http://imgs.xkcd.com/comics/diacritics.png', 'day': '24', 'safe_title': 'Diacritics', 'news': '', 'title': 'Diacritics'}
This is easier with the requests module:
import requests
response = requests.get('http://xkcd.com/info.0.json')
data = response.json()
>>> data
{'link': '', 'transcript': '', 'month': '2', 'year': '2016', 'alt': 'Using diacritics correctly is not my forté.', 'num': 1647, 'img': 'http://imgs.xkcd.com/comics/diacritics.png', 'day': '24', 'safe_title': 'Diacritics', 'news': '', 'title': 'Diacritics'}
requests saves you the hassle of decoding the incoming data and decoding the JSON.
In Python 2.7 you need to import urllib2 and then import json to load the data into a variable as a Python dictionary. Resource here
import urllib2
import json
response = urllib2.urlopen('http://xkcd.com/info.0.json')
html = response.read().decode('utf8')
data = json.loads(html)
type(data) is dict # True

Categories

Resources