Get specific attribute data from Json - python

I'm implemented a code to return Json value like this
{ "key1":"1", "key2":"2", ......}
But I want to get only value of key1. so I first defined info to get Json.
info = requests.get(url, headers=headers)
And use info.text['key1'] to get value of key1. but i got error. could anyone suggest any solution?

JSON is a format, but the data are included as text. Inside the info.text you have a string.
If you want to access the json data you can do info.json()['key1']. This will work only if the response content type is defined as JSON, to check that do
info.headers['content-type'] should be application/json; charset=utf8
http://docs.python-requests.org/en/master/
Otherwise you will have to manually load the text to json, with json library for example
import json
response = requests.get(url, headers=headers)
data = json.loads(response.text)

import json
info = requests.get(url, headers=headers)
jsonObject = json.loads(info)
key1= jsonObject['key1']
or
jsonObject = info.json()
key1= jsonObject['key1']

Related

How to Itreate through Json

I need [0] to increase everytime and fetch the data when index change. from 0 to 13
import requests as r
import json
url = "https://services6.arcgis.com/bKYAIlQgwHslVRaK/arcgis/rest/services/CasesByRegion_ViewLayer/FeatureServer/0/query?where=1%3D1&outFields=*&outSR=4326&f=json"
response = urlopen(url)
Data= json.load(response )
for index in Data:
list = Data['features'][0]['attributes']
[0]+1
print(list)
Here is another simple approach without using urllib:
import requests as r
import json
url = "https://jsonplaceholder.typicode.com/todos/1"
response = r.get(url)
data = response.json()
print(data)
requests.get().json() delivers the complete dict from the response payload:
import requests as r
response = r.get(url)
Data = response.json()
Your json.load() doesn't work as expected because response is a dictionary from the requests module, containing some HTTP stuff like status code, reason, encoding. For API calls, this is not what you want (HTTP errors should be handled with exceptions). What you want is response.json() or response.text.
Also, you imported requests but didn't use it? I don't know about urlopen(). Use requests.get().

How to get JSON response in Python?

I've been trying to get JSON response out of the following code, but it's throwing an error
from requests import get
import json
url = "https://api.wheretheiss.at/v1/satellites/25544"
response = get(url)
for item in response:
print(item, response[item])
I wanna print the JSON in the following format:
https://i.stack.imgur.com/ZWMbr.png
your code works well to get the correct respone from the url. The problem is that you should parse the raw respone as the json format. Please refer to the following code:
from requests import get
import json
url = "https://api.wheretheiss.at/v1/satellites/25544"
response = get(url)
# dump response into json
result = response.content.decode('utf-8')
result = json.loads(result)
print(result) # print raw json
# print as key-value pairs
for item in result:
print("{0} : {1}".format(item, result[item]))
The output is like following:
Example output
I think you're misunderstanding what the response contains.
Your initial code is good.
from requests import get
import json
url = "https://api.wheretheiss.at/v1/satellites/25544"
response = get(url)
You now have a response. see here for more info.
In this response you have status_code, content, encoding, and the response content as text. it also contains other information about the request. eg we don't want to continue if the request failed
You can simply parse the json by calling json.loads on the decoded response via response.text
parsed_json = json.loads(response.text)
or
parsed_json = response.json()
for item in json_:
print(item, json_[item])
I hope it helps. it is also duplicate to HTTP requests and JSON parsing in Python

How to get value from JSON format

I am using Python request module to get API response. The response is should be JSON format. From the response how do I retrieve the specific value?
Example of API response:
{
id: 2337975,
sha: "eac6910f89883110e673db27456b67f542df6d75",
ref: "mail-gun",
status: "manual",
created_at: "2021-03-01T09:15:02.409Z",
updated_at: "2021-03-01T09:19:14.983Z",
web_url: "https://gitlab.com/optimus/optimus-ci/-/pipelines/2337975"
}
From here I want retrieve on ID :2337975 assign into a variable in Python.
Here is my code
url = f'https://gitlab.com/api/v4/projects/{pid}/pipelines?updated_after={update_after}&ref={branch}&status=manual'
headers = {'Authorization' : 'Bearer xxxxxxxx'}
response = requests.get(url, headers=headers)
output = json.loads(response.text)
print(output)
I can print the whole JSON format by print(output), but I only want to get a Id value.
Anybody can help?
change this:
output = json.loads(response.text)
to this: (using json function you can receive the json response in string format)
load the response into a python dictionary
response_json = json.loads(response.json())
and access the id key from response dictionary
variable = response_json["id"]
You should save this JSON code in a .json file, then you can open it, load it and then use variable ["id"].
parse the json and use the id as key in json to extract.
loaded_json = json.loads(json_data)
for x in loaded_json:
print("%s: %d" % (x, loaded_json[id]))
since the returned value of a json is an object (JavaScript Object Notation) you can treat it as such and just destructure the object with the [] notation as other pointed out response_json["id"]
I solve this using a naive way. which is convert the object to JSON > python list > python dictionary
response = requests.get(url, headers=headers, proxies=proxyDict)
response_list = response.json()
response_dict = response_list[0]
print(response_dict["id"])

How to access certain items from a python requests response?

I am new to python requests and I am unsure how to access one singular item from the response rather than all of them. Any help would be appreciated. I would like to be able to print the kdRatio, there is multiple different kdRatios in the response but I was looking for the one following lifetime and All and Properties. Thanks
import requests as rq
header = {
'x-rapidapi-host': "call-of-duty-modern-warfare.p.rapidapi.com",
'x-rapidapi-key': "0beff1d7acmsh29065741543208dp152ecbjsnb56e8654cc77"
}
response = rq.get('https://call-of-duty-modern-
warfare.p.rapidapi.com/multiplayer/%25C7%259Ep%25C3%25ABx%25231878/battle', headers=header)
print(response)
print(response.text)
Here is the response I am getting.
<Response [200]>
{"title":"mw","platform":"battle","username":"Ǟpëx#1878","type":"mp","level":55,"maxLevel":0,"levelXpRemainder":3000,"levelXpGained":7000,"prestige":0,"prestigeId":0,"maxPrestige":0,"totalXp":960000,"paragonRank":0,"paragonId":0,"s":1,"p":1,"lifetime":{"all":{"properties":{"recordLongestWinStreak":14,"recordXpInAMatch":29276,"accuracy":0.2253633737564087,"losses":456,"totalGamesPlayed":1216,"score":2853122,"winLossRatio":1.094298243522644,"totalShots":286484,"bestScoreXp":0,"gamesPlayed":1216,"bestSquardKills":0,"bestSguardWave":0,"bestConfirmed":19,"deaths":13057,"wins":499,"bestSquardCrates":0,"kdRatio":1.495213270187378,"bestAssists":27,"bestFieldgoals":0,"bestScore":16625,"recordDeathsInAMatch":55,"scorePerGame":2346.3174342105262,"bestSPM":2450,"bestKillChains":0,"recordKillsInAMatch":113,"suicides":79,"wlRatio":1.094298243522644,"currentWinStreak":2,"bestMatchBonusXp":0,"bestMatchXp":0,"bestSguardWeaponLevel":0,"bestKD":16,"kills":19523,"bestKillsAsInfected":3,"bestReturns":0,"bestStabs":0,"bestKillsAsSurvivor":10,"timePlayedTotal":488334,"bestDestructions":1,"headshots":3303,"bestRescues":5,"assists":3237,"ties":1,"recordKillStreak":29,"bestPlants":1,"misses":221921,"bestDamage":0,"bestSetbacks":0,"bestTouchdowns":0,"scorePerMinute":350.5537603361634,"bestDeaths":55,"bestMedalXp":0,"bestDefends":41,"bestSquardRevives":0,"bestKills":113,"bestDefuses":0,"bestCaptures":8,"hits":64563,"bestKillStreak":29,"bestDenied":8}},"mode":{"gun":{"properties":{"kills":12,"score":1262,"timePlayed":311,"kdRatio":0.9230769230769231,"setBacks":0,"scorePerMinute":243.47266881028938,"stabs":0,"deaths":13}},"dom":{"properties":{"kills":3986,"score":466680,"timePlayed":92224,"kdRatio":1.619008935824533,"captures":228,"defends":412,"scorePerMinute":303.617279666898,"deaths":2462}},"war":{"properties":{"kills":1292,"score":165815,"timePlayed":25846,"kdRatio":1.453318335208099,"assists":271,"scorePerMinute":384.92996982124896,"deaths":889}},"hq":{"properties":{"kills":4111,"score":441585,"timePlayed":99696,"kdRatio":1.6563255439161966,"captures":93,"defends":27,"scorePerMinute":265.7589070775157,"deaths":2482}},"hc_dom":{"properties":{"kills":942,"score":103870,"timePlayed":23342,"kdRatio":1.3792093704245973,"captures":46,"defends":81,"scorePerMinute":266.9951160997344,"deaths":683}},"hc_conf":{"properties":{"kills":73,"score":8300,"timePlayed":1367,"kdRatio":1.2372881355932204,"confirms":15,"scorePerMinute":364.3013899049012,"denies":20,"deaths":59}},"koth":{"properties":{"kills":1311,"score":187420,"timePlayed":27342,"kdRatio":1.4219088937093276,"defends":37,"objTime":697,"scorePerMinute":411.27935044985736,"deaths":922}},"conf":{"properties":{"kills":1042,"score":146180,"timePlayed":19643,"kdRatio":1.507959479015919,"confirms":273,"scorePerMinute":446.5102071984931,"denies":104,"deaths":691}},"hc_hq":{"properties":{"kills":166,"score":17735,"timePlayed":2726,"kdRatio":1.2296296296296296,"captures":2,"defends":1,"scorePerMinute":390.35216434336024,"deaths":135}},"arena":{"properties":{"kills":815,"score":143855,"timePlayed":41493,"damage":97577,"kdRatio":1.1256906077348066,"assists":193,"scorePerMinute":208.0182199407129,"deaths":724}},"br_dmz":{"properties":{"wins":0,"kills":7,"kdRatio":3.5,"downs":9,"topTwentyFive":0,"topTen":0,"contracts":0,"revives":0,"topFive":0,"score":3312,"timePlayed":1108,"gamesPlayed":1,"tokens":0,"scorePerMinute":179.35018050541518,"cash":0,"deaths":2}},"br":{"properties":{"wins":5,"kills":736,"kdRatio":1.6919540229885057,"downs":789,"topTwentyFive":117,"topTen":54,"contracts":104,"revives":100,"topFive":27,"score":713500,"timePlayed":169249,"gamesPlayed":146,"tokens":0,"scorePerMinute":252.94093318128913,"cash":0,"deaths":435}},"sd":{"properties":{"kills":146,"score":71070,"timePlayed":44172,"kdRatio":1.4174757281553398,"plants":0,"scorePerMinute":96.5362673186634,"defuses":2,"deaths":103}},"grnd":{"properties":{"kills":0,"score":1875,"timePlayed":292,"kdRatio":0,"defends":0,"objTime":0,"scorePerMinute":385.2739726027398,"deaths":0}},"cyber":{"properties":{"kills":70,"score":37335,"timePlayed":20866,"kdRatio":1.206896551724138,"plants":1,"scorePerMinute":107.35646506278157,"revives":0,"deaths":58}},"hc_war":{"properties":{"kills":0,"score":625,"timePlayed":222,"kdRatio":0,"assists":0,"scorePerMinute":168.9189189189189,"deaths":0}},"br_all":{"properties":{"wins":5,"kills":743,"kdRatio":1.700228832951945,"downs":798,"topTwentyFive":117,"topTen":54,"contracts":104,"revives":100,"topFive":27,"score":716812,"timePlayed":170357,"gamesPlayed":147,"tokens":0,"scorePerMinute":252.46229975874195,"cash":0,"deaths":437}},"hc_sd":{"properties":{"kills":0,"score":0,"timePlayed":0,"kdRatio":0,"plants":0,"scorePerMinute":0,"defuses":0,"deaths":0}},"arm":{"properties":{"kills":526,"score":76725,"timePlayed":25625,"kdRatio":1.1311827956989247,"captures":27,"defends":16,"scorePerMinute":179.64878048780488,"deaths":465}},"hc_cyber":{"properties":{"kills":0,"score":0,"timePlayed":0,"kdRatio":0,"plants":0,"scorePerMinute":0,"revives":0,"deaths":0}},"infect":{"properties":{"kills":67,"score":28425,"infected":17,"timePlayed":4649,"kdRatio":0.9305555555555556,"scorePerMinute":366.85308668530865,"time":2641,"deaths":72}}},"map":{},"itemData":{"weapon_assault_rifle":{"iw8_ar_tango21":{"properties":{"hits":41,"kills":8,"kdRatio":8,"headshots":1,"accuracy":0.16141732283464566,"shots":254,"deaths":0}},"iw8_ar_mike4":{"properties":{"hits":11685,"kills":2576,"kdRatio":1.4015233949945594,"headshots":471,"accuracy":0.2240136497833672,"shots":52162,"deaths":1838}},"iw8_ar_valpha":{"properties":{"hits":0,"kills":0,"kdRatio":0,"headshots":0,"accuracy":0,"shots":0,"deaths":0}},"iw8_ar_falpha":{"properties":{"hits":54,"kills":14,"kdRatio":1.5555555555555556,"headshots":5,"accuracy":0.18305084745762712,"shots":295,"deaths":9}},"iw8_ar_mcharlie":{"properties":{"hits":870,"kills":141,"kdRatio":1.3689320388349515,"headshots":35,"accuracy":0.223305954825462,"shots":3896,"deaths":103}},"iw8_ar_akilo47":{"properties":{"hits":3741,"kills":781,"kdRatio":1.3259762308998302,"headshots":137,"accuracy":0.26047904191616766,"shots":14362,"deaths":589}},"iw8_ar_asierra12":{"properties":{"hits":456,"kills":151,"kdRatio":1.26890756302521,"headshots":33,"accuracy":0.22233057045343735,"shots":2051,"deaths":119}},"iw8_ar_galima":{"properties":{"hits":3,"kills":1,"kdRatio":1,"headshots":0,"accuracy":0.23076923076923078,"shots":13,"deaths":0}},"iw8_ar_sierra552":{"properties":{"hits":1611,"kills":353,"kdRatio":1.6045454545454545,"headshots":48,"accuracy":0.23719081272084805,"shots":6792,"deaths":220}},"iw8_ar_falima":{"properties":{"hits":2579,"kills":1010,"kdRatio":1.6476345840130506,"headshots":208,"accuracy":0.26316326530612244,"shots":9800,"deaths":613}},"iw8_ar_anovember94":{"properties":{"hits":24,"kills":4,"kdRatio":0.8,"headshots":3,"accuracy":0.13114754098360656,"shots":183,"deaths":5}},"iw8_ar_kilo433":{"properties":{"hits":5034,"kills":1034,"kdRatio":1.267156862745098,"headshots":179,"accuracy":0.2387818992505455,"shots":21082,"deaths":816}},"iw8_ar_scharlie":{"properties":{"hits":326,"kills":91,"kdRatio":1.3582089552238805,"headshots":12,"accuracy":0.24847560975609756,"shots":1312,"deaths":67}}},"weapon_shotgun":{"iw8_sh_mike26":{"properties":{"hits":5,"kills":1,"kdRatio":1,"headshots":0,"accuracy":0.3125,"shots":16,"deaths":0}},"iw8_sh_charlie725":{"properties":{"hits":1538,"kills":1195,"kdRatio":1.3307349665924275,"headshots":187,"accuracy":0.694043321299639,"shots":2216,"deaths":898}},"iw8_sh_oscar12":{"properties":{"hits":973,"kills":441,"kdRatio":1.1221374045801527,"headshots":45,"accuracy":0.43053097345132746,"shots":2260,"deaths":393}},"iw8_sh_romeo870":{"properties":{"hits":58,"kills":28,"kdRatio":0.8,"headshots":4,"accuracy":0.5272727272727272,"shots":110,"deaths":35}},"iw8_sh_dpapa12":{"properties":{"hits":20,"kills":9,"kdRatio":0.6428571428571429,"headshots":0,"accuracy":0.32786885245901637,"shots":61,"deaths":14}}},"weapon_marksman":{"iw8_sn_sbeta":{"properties":{"hits":192,"kills":143,"kdRatio":0.9930555555555556,"headshots":35,"accuracy":0.3742690058479532,"shots":513,"deaths":144}},"iw8_sn_crossbow":{"properties":{"hits":4,"kills":7,"kdRatio":1.4,"headshots":2,"accuracy":0.125,"shots":32,"deaths":5}},"iw8_sn_kilo98":{"properties":{"hits":76,"kills":70,"kdRatio":1.1666666666666667,"headshots":32,"accuracy":0.4342857142857143,"shots":175,"deaths":60}},"iw8_sn_mike14":{"properties":{"hits":52,"kills":27,"kdRatio":1.9285714285714286,"headshots":12,"accuracy":0.23318385650224216,"shots":223,"deaths":14}},"iw8_sn_sksierra":{"properties":{"hits":26,"kills":9,"kdRatio":9,"headshots":5,"accuracy":0.2857142857142857,"shots":91,"deaths":0}}},"weapon_sniper":{"iw8_sn_romeo700":{"properties":{"hits":0,"kills":0,"kdRatio":0,"headshots":0,"accuracy":0,"shots":0,"deaths":0}},"iw8_sn_alpha50":{"properties":{"hits":138,"kills":168,"kdRatio":1.3658536585365855,"headshots":41,"accuracy":0.36507936507936506,"shots":378,"deaths":123}},"iw8_sn_hdromeo":{"properties":{"hits":22,"kills":27,"kdRatio":0.8181818181818182,"headshots":5,"accuracy":0.3188405797101449,"shots":69,"deaths":33}},"iw8_sn_delta":{"properties":{"hits":79,"kills":51,"kdRatio":1,"headshots":15,"accuracy":0.30268199233716475,"shots":261,"deaths":51}},"iw8_sn_xmike109":{"properties":{"hits":1,"kills":1,"kdRatio":1,"headshots":0,"accuracy":1,"shots":1,"deaths":0}}},"tacticals":{"equip_gas_grenade":{"properties":{"extraStat1":1,"uses":19}},"equip_snapshot_grenade":{"properties":{"extraStat1":471,"uses":979}},"equip_decoy":{"properties":{"extraStat1":95,"uses":15}},"equip_smoke":{"properties":{"extraStat1":0,"uses":157}},"equip_concussion":{"properties":{"extraStat1":280,"uses":660}},"equip_hb_sensor":{"properties":{"extraStat1":0,"uses":98}},"equip_flash":{"properties":{"extraStat1":101,"uses":283}},"equip_adrenaline":{"properties":{"extraStat1":142214,"uses":2505}}},"lethals":{"equip_frag":{"properties":{"kills":41,"uses":556}},"equip_thermite":{"properties":{"kills":2,"uses":25}},"equip_semtex":{"properties":{"kills":6,"uses":71}},"equip_claymore":{"properties":{"kills":225,"uses":1509}},"equip_c4":{"properties":{"kills":57,"uses":664}},"equip_at_mine":{"properties":{"kills":285,"uses":2563}},"equip_throwing_knife":{"properties":{"kills":5,"uses":30}},"equip_molotov":{"properties":{"kills":24,"uses":465}}},"weapon_lmg":{"iw8_lm_kilo121":{"properties":{"hits":218,"kills":59,"kdRatio":1.5128205128205128,"headshots":13,"accuracy":0.15900802334062727,"shots":1371,"deaths":39}},"iw8_lm_mkilo3":{"properties":{"hits":441,"kills":105,"kdRatio":1.09375,"headshots":18,"accuracy":0.17904993909866018,"shots":2463,"deaths":96}},"iw8_lm_mgolf34":{"properties":{"hits":64,"kills":20,"kdRatio":1.8181818181818181,"headshots":6,"accuracy":0.17344173441734417,"shots":369,"deaths":11}},"iw8_lm_lima86":{"properties":{"hits":150,"kills":25,"kdRatio":1.25,"headshots":8,"accuracy":0.20080321285140562,"shots":747,"deaths":20}},"iw8_lm_pkilo":{"properties":{"hits":1390,"kills":342,"kdRatio":1.1324503311258278,"headshots":62,"accuracy":0.15873015873015872,"shots":8757,"deaths":302}},"iw8_lm_sierrax":{"properties":{"hits":1875,"kills":429,"kdRatio":1.1230366492146597,"headshots":63,"accuracy":0.22278992395437264,"shots":8416,"deaths":382}},"iw8_lm_mgolf36":{"properties":{"hits":1756,"kills":388,"kdRatio":1.065934065934066,"headshots":88,"accuracy":0.20223425083496488,"shots":8683,"deaths":364}}},"weapon_launcher":{"iw8_la_gromeo":{"properties":{"hits":23,"kills":0,"kdRatio":0,"headshots":0,"accuracy":0.45098039215686275,"shots":51,"deaths":26}},"iw8_la_rpapa7":{"properties":{"hits":3,"kills":51,"kdRatio":1.1590909090909092,"headshots":0,"accuracy":0.02040816326530612,"shots":147,"deaths":44}},"iw8_la_juliet":{"properties":{"hits":8,"kills":0,"kdRatio":0,"headshots":0,"accuracy":0.24242424242424243,"shots":33,"deaths":0}},"iw8_la_kgolf":{"properties":{"hits":0,"kills":0,"kdRatio":0,"headshots":0,"accuracy":0,"shots":0,"deaths":0}},"iw8_la_mike32":{"properties":{"hits":0,"kills":0,"kdRatio":0,"headshots":0,"accuracy":0,"shots":14,"deaths":0}}},"supers":{"super_emp_drone":{"properties":{"kills":0,"misc1":0,"misc2":0,"uses":0}},"super_trophy":{"properties":{"kills":0,"misc1":33,"misc2":0,"uses":32}},"super_ammo_drop":{"properties":{"kills":1,"misc1":207,"misc2":0,"uses":108}},"super_weapon_drop":{"properties":{"kills":0,"misc1":2,"misc2":0,"uses":1}},"super_fulton":{"properties":{"kills":0,"misc1":0,"misc2":0,"uses":0}},"super_armor_drop":{"properties":{"kills":0,"misc1":0,"misc2":0,"uses":24}},"super_select":{"properties":{"kills":0,"misc1":0,"misc2":0,"uses":303}},"super_tac_insert":{"properties":{"kills":0,"misc1":2,"misc2":0,"uses":2}},"super_recon_drone":{"properties":{"kills":0,"misc1":5,"misc2":0,"uses":5}},"super_deadsilence":{"properties":{"kills":616,"misc1":9,"misc2":0,"uses":474}},"super_supply_drop":{"properties":{"kills":0,"misc1":0,"misc2":0,"uses":45}},"super_tac_cover":{"properties":{"kills":0,"misc1":807,"misc2":0,"uses":33}},"super_support_box":{"properties":{"kills":29,"misc1":0,"misc2":0,"uses":34}}},"weapon_pistol":{"iw8_pi_cpapa":{"properties":{"hits":91,"kills":50,"kdRatio":0.7142857142857143,"headshots":11,"accuracy":0.20634920634920634,"shots":441,"deaths":70}},"iw8_pi_mike9":{"properties":{"hits":12,"kills":3,"kdRatio":3,"headshots":1,"accuracy":0.11764705882352941,"shots":102,"deaths":0}},"iw8_pi_mike1911":{"properties":{"hits":91,"kills":18,"kdRatio":0.8571428571428571,"headshots":2,"accuracy":0.1961206896551724,"shots":464,"deaths":21}},"iw8_pi_golf21":{"properties":{"hits":219,"kills":26,"kdRatio":1.1818181818181819,"headshots":4,"accuracy":0.26290516206482595,"shots":833,"deaths":22}},"iw8_pi_decho":{"properties":{"hits":203,"kills":73,"kdRatio":0.7019230769230769,"headshots":15,"accuracy":0.3481989708404803,"shots":583,"deaths":104}},"iw8_pi_papa320":{"properties":{"hits":87,"kills":16,"kdRatio":0.4444444444444444,"headshots":5,"accuracy":0.17864476386036962,"shots":487,"deaths":36}}},"weapon_other":{"iw8_me_riotshield":{"properties":{"hits":0,"kills":2,"kdRatio":0.5,"headshots":0,"accuracy":0,"shots":0,"deaths":4}}},"weapon_smg":{"iw8_sm_mpapa7":{"properties":{"hits":5392,"kills":944,"kdRatio":1.3964497041420119,"headshots":194,"accuracy":0.255315119087078,"shots":21119,"deaths":676}},"iw8_sm_augolf":{"properties":{"hits":6903,"kills":1747,"kdRatio":1.373427672955975,"headshots":290,"accuracy":0.23947131062235483,"shots":28826,"deaths":1272}},"iw8_sm_papa90":{"properties":{"hits":3969,"kills":746,"kdRatio":1.231023102310231,"headshots":180,"accuracy":0.22036533229692964,"shots":18011,"deaths":606}},"iw8_sm_charlie9":{"properties":{"hits":0,"kills":0,"kdRatio":0,"headshots":0,"accuracy":0,"shots":0,"deaths":0}},"iw8_sm_mpapa5":{"properties":{"hits":6885,"kills":1647,"kdRatio":1.5082417582417582,"headshots":283,"accuracy":0.22102728731942214,"shots":31150,"deaths":1092}},"iw8_sm_smgolf45":{"properties":{"hits":24,"kills":5,"kdRatio":5,"headshots":1,"accuracy":0.23300970873786409,"shots":103,"deaths":0}},"iw8_sm_beta":{"properties":{"hits":3053,"kills":686,"kdRatio":1.4503171247357294,"headshots":126,"accuracy":0.2310253499810821,"shots":13215,"deaths":473}},"iw8_sm_victor":{"properties":{"hits":61,"kills":11,"kdRatio":0.6111111111111112,"headshots":2,"accuracy":0.23371647509578544,"shots":261,"deaths":18}},"iw8_sm_uzulu":{"properties":{"hits":3345,"kills":989,"kdRatio":1.356652949245542,"headshots":184,"accuracy":0.21162849550803492,"shots":15806,"deaths":729}}},"weapon_melee":{"iw8_me_akimboblunt":{"properties":{"hits":0,"kills":0,"kdRatio":0,"headshots":0,"accuracy":0,"shots":0,"deaths":0}},"iw8_me_akimboblades":{"properties":{"hits":0,"kills":0,"kdRatio":0,"headshots":0,"accuracy":0,"shots":0,"deaths":0}},"iw8_knife":{"properties":{"hits":0,"kills":9,"kdRatio":0.17307692307692307,"headshots":0,"accuracy":0,"shots":0,"deaths":52}}}},"scorestreakData":{"lethalScorestreakData":{"precision_airstrike":{"properties":{"extraStat1":92,"uses":110,"awardedCount":96}},"cruise_predator":{"properties":{"extraStat1":13,"uses":12,"awardedCount":0}},"manual_turret":{"properties":{"extraStat1":0,"uses":3,"awardedCount":0}},"white_phosphorus":{"properties":{"extraStat1":3,"uses":1,"awardedCount":1}},"hover_jet":{"properties":{"extraStat1":482,"uses":176,"awardedCount":177}},"chopper_gunner":{"properties":{"extraStat1":507,"uses":72,"awardedCount":77}},"gunship":{"properties":{"extraStat1":19,"uses":3,"awardedCount":3}},"sentry_gun":{"properties":{"extraStat1":0,"uses":7,"awardedCount":4}},"toma_strike":{"properties":{"extraStat1":17,"uses":39,"awardedCount":25}},"nuke":{"properties":{"extraStat1":0,"uses":0,"awardedCount":0}},"juggernaut":{"properties":{"extraStat1":0,"uses":0,"awardedCount":0}},"pac_sentry":{"properties":{"extraStat1":0,"uses":0,"awardedCount":0}},"chopper_support":{"properties":{"extraStat1":2,"uses":1,"awardedCount":1}},"bradley":{"properties":{"extraStat1":0,"uses":0,"awardedCount":0}}},"supportScorestreakData":{"airdrop":{"properties":{"extraStat1":0,"uses":48,"awardedCount":55}},"radar_drone_overwatch":{"properties":{"extraStat1":0,"uses":66,"awardedCount":64}},"scrambler_drone_guard":{"properties":{"extraStat1":0,"uses":5,"awardedCount":0}},"uav":{"properties":{"extraStat1":5675,"uses":1194,"awardedCount":1151}},"airdrop_multiple":{"properties":{"extraStat1":0,"uses":0,"awardedCount":0}},"directional_uav":{"properties":{"extraStat1":13,"uses":2,"awardedCount":1}}}},"accoladeData":{"properties":{"classChanges":138,"highestAvgAltitude":225,"killsFromBehind":203,"lmgDeaths":78,"riotShieldDamageAbsorbed":5,"flashbangHits":24,"meleeKills":34,"tagsLargestBank":0,"shotgunKills":109,"sniperDeaths":99,"timeProne":80,"killstreakWhitePhosphorousKillsAssists":1,"shortestLife":80,"deathsFromBehind":112,"higherRankedKills":181,"mostAssists":142,"leastKills":55,"tagsDenied":0,"killstreakWheelsonKills":0,"sniperHeadshots":41,"killstreakJuggernautKills":0,"smokesUsed":15,"avengerKills":129,"decoyHits":6,"killstreakCarePackageUsed":0,"molotovKills":15,"gasHits":0,"comebackKills":77,"lmgHeadshots":49,"smgDeaths":96,"carrierKills":0,"deployableCoverUsed":18,"thermiteKills":2,"arKills":137,"c4Kills":0,"suicides":47,"clutch":4,"survivorKills":0,"killstreakGunshipKills":2,"timeSpentAsPassenger":3,"returns":0,"smgHeadshots":169,"launcherDeaths":3,"oneShotOneKills":100,"ammoBoxUsed":1,"spawnSelectSquad":1,"weaponPickups":128,"pointBlankKills":200,"tagsCaptured":0,"killstreakGroundKills":0,"distanceTraveledInVehicle":2,"longestLife":139,"stunHits":56,"spawnSelectFlag":0,"shotgunHeadshots":92,"bombDefused":5,"snapshotHits":110,"noKillsWithDeath":3,"killstreakAUAVAssists":2,"killstreakPersonalUAVKills":13,"tacticalInsertionSpawns":63,"launcherKills":2,"spawnSelectVehicle":2,"mostKillsLeastDeaths":62,"mostKills":201,"defends":34,"timeSpentAsDriver":1,"bombDetonated":2,"arHeadshots":123,"timeOnPoint":0,"lmgKills":59,"killstreakUAVAssists":179,"carepackagesCaptured":24,"mostKillsLongestStreak":133,"killstreakCruiseMissileKills":6,"longestStreak":221,"destroyedKillstreaks":18,"hipfireKills":192,"stimDamageHealed":137,"skippedKillcams":76,"leastAssists":198,"mostMultikills":207,"highestRankedKills":179,"killstreakAirstrikeKills":34,"distanceTravelled":114,"killstreakKills":119,"semtexKills":6,"penetrationKills":128,"explosionsSurvived":104,"highestMultikill":241,"arDeaths":78,"longshotKills":231,"proximityMineKills":126,"tagsMegaBanked":0,"mostKillsMostHeadshots":95,"firstInfected":1,"killstreakCUAVAssists":3,"throwingKnifeKills":3,"executionKills":1,"lastSurvivor":0,"reconDroneMarks":0,"deadSilenceKills":58,"revengeKills":164,"infectedKills":1,"killEnemyTeam":186,"sniperKills":63,"killstreakCluserStrikeKills":7,"meleeDeaths":57,"timeWatchingKillcams":27,"killstreakTankKills":0,"noKillNoDeath":5,"shotgunDeaths":87,"killstreakChopperGunnerKills":67,"shotsFired":120,"stoppingPowerKills":3,"pistolPeaths":29,"killstreakShieldTurretKills":0,"timeCrouched":39,"noDeathsFromBehind":855,"bombPlanted":4,"setbacks":0,"smgKills":187,"claymoreKills":66,"kills10NoDeaths":1,"pistolHeadshots":20,"killstreakVTOLJetKills":105,"headshots":219,"mostDeaths":68,"adsKills":153,"empDroneHits":0,"defenderKills":157,"launcherHeadshots":2,"timesSelectedAsSquadLeader":2,"killstreakAirKills":124,"assaults":1,"fragKills":19,"killstreakEmergencyAirdropUsed":0,"captures":16,"killstreakChopperSupportKills":1,"spawnSelectBase":0,"noKill10Deaths":0,"leastDeaths":181,"killstreakSentryGunKills":0,"longestTimeSpentOnWeapon":0,"lowerRankedKills":171,"trophySystemHits":6,"clutchRevives":0,"lowestAvgAltitude":55,"pickups":0,"pistolKills":47,"reloads":134}}},"weekly":{"all":{"properties":null},"mode":{},"map":{}},"engagement":null}
Parse the JSON response with response.json(), then access by key:
data = response.json()
kd_ratio = data['lifetime']['all']['properties']['kdRatio']
Here you can take the JSON response instead of a text response.
import requests as rq
header = {
'x-rapidapi-host': "call-of-duty-modern-warfare.p.rapidapi.com",
'x-rapidapi-key': "0beff1d7acmsh29065741543208dp152ecbjsnb56e8654cc77"
}
response = rq.get('https://call-of-duty-modern-
warfare.p.rapidapi.com/multiplayer/%25C7%259Ep%25C3%25ABx%25231878/battle', headers=header)
print(response)
print(response.json()['lifetime']['all']['properties']['kdRatio'])
For better understanding:
data = response.json()
print(data.keys()) # will print all the keys
print(type(data['all'])) # checked the type.
# since it is dict
print(data['all'].keys())
print(data['all']['properties'])
print(data['all']['properties'].keys())
print(data['all']['properties']['kdRatio'])
Try this in ipython you will get more idea about json data

Grab data from bitfinex with Python

I'm using this :
import requests
url = "https://api.bitfinex.com/v1/pubticker/btcusd"
response = requests.request("GET", url)
print (response.text)
The output is:
{"mid":"4432.95","bid":"4432.9","ask":"4433.0","last_price":"4432.9","low":"4276.9","high":"4482.0","volume":"32877.86104158","timestamp":"1506955900.864889"}
I would like to grab the last price, as a float, in order to use it for some calculations....
I cannot figure out how to do that...
Can you help me please ?
Thx
Try like this:
import requests
url = "https://api.bitfinex.com/v1/pubticker/btcusd"
response = requests.request("GET", url)
data = response.json()
last_price = float(data.get('last_price'))
print(last_price)
Response is a JSON so requests will do the job of parsing it for you, if you call the .json() method on your response. You just need to do:
import requests
resp_json = requests.get("https://api.bitfinex.com/v1/pubticker/btcusd").json()
# resp_json is a native Python dict representing the JSON response content. But since all values are strings in the response, you need to convert them to a float manually.
print(float(resp_json["last_price"]))

Categories

Resources