I am trying to decode a JSON with Python. Here is a little snippet of what the JSON looks like.
b'{"success":true,"data":[{"id":26,"name":"A","comment":"","start_time_plan":null,"start_time_actual":"2016-09-13 00:00:00","start_time_delta":null,"start_time_score":null,"start_time_score_achievement":null,"start_time_traffic_light":null,"end_time_plan":null,"end_time_actual":"2016-09-13 00:00:00","end_time_delta":null,"end_time_score":null,"end_time_score_achievement":null,"end_time_traffic_light":null,"status":0,"measure_schedule_revision_id":63,"responsible_user_id":3,"created_time":"2016-09-13 11:29:14","created_user_id":3,"modified_time":"2016-09-21 16:33:41","modified_user_id":3,"model":"Activity"},{"id":27,"name":"B","comment":"","start_time_plan":null,"start_time_actual":"2016-09-13 00:00:00","start_time_delta":null,"start_time_score":null,"start_time_score_achievement":null,"start_time_traffic_light":null,"end_time_plan":null,"end_time_actual":"2016-09-13 00:00:00","end_time_delta":null,"end_time_score":null,"end_time_score_achievement":null,"end_time_traffic_light":null,"status":0,"measure_schedule_revision_id":63,"responsible_user_id":3,"created_time":"2016-09-13 11:29:48","created_user_id":3,"modified_time":"2016-10-16 18:14:36","modified_user_id":1,"model":"Activity"}
I am trying to get a hold of start_time_deltaand end_time_delta and produce a little scatter plot. But somehow I can't decode the JSON.
Here is what I do:
#falcon api
u = 'https://myurl.com'
#urllib3 + poolmanager for requests
import urllib3
http = urllib3.PoolManager()
import json
r = http.request('GET', u)
json.loads(r.data.decode('utf-8'))
end = json.loads(r.data['end_time_delta'])
start = json.loads(r.data['start_time_delta'])
This is the error I get: byte indices must be integers or slices, not str
How come? And how do I solve the problem?
You are ignoring the return value of json.loads() here:
json.loads(r.data.decode('utf-8'))
You then try to decode the same raw again and try to use that as the decoded Python result. Call json.loads() once, and use the resulting Python dictionary:
result = json.loads(r.data.decode('utf-8'))
start = result['data'][0]['start_time_delta']
end = result['data'][0]['end_time_delta']
Because top-level dictionary 'data' key points to a list of results, I used 0 to get to the first of those and extract the data you want.
If you need to extract those data points for every dictionary in that list, you'd have to use a loop:
for entry in result['data']:
start = entry['start_time_delta']
end = entry['end_time_delta']
# do something with these two values, before iterating to the next
Related
I tried below code but it gave
TypeError: string indices must be integers
from nsepython import *
import pandas as pd
import json
positions = nse_optionchain_scrapper ('HDFC')
json_decode = json.dumps(positions,indent = 4, sort_keys=True,separators =(". ", " = "))
print(json_decode['data'][0]['identifier'])
print(json_decode['filtered']['data'][0]['PE']['identifier'])
json_decode = json.dumps(positions,indent = 4, sort_keys=True,separators =(". ", " = "))
You can't build a JSON string (which is what json.dumps does) and then try to access part of the result as if it were the original data structure. Your json_decode is just a string, not a dict; as far as Python is concerned it has no structure beyond the individual characters that make it up.
If you want to access parts of the data, just use positions directly:
print(positions['data'][0]['identifier'])
You can encode just that bit to JSON if you like:
print(json.dumps(positions['data'][0]['identifier'])
but that's probably just a quoted string in this case.
So I'm not sure what your goal is. If you want to print out the JSON version of positions, great, just print it out. But the JSON form is for input and output only; it's not suitable for messing around with inside your Python code.
I am trying to parse a "complicated" JSON string that is returned to me by an API.
It looks like this:
{
"data":[
["Distance to last strike","23.0","miles"],
["Time of last strike","1/14/2022 9:23:42 AM",""],
["Number of strikes today","1",""]
]
}
While the end goal will be to extract the distance, date/time, as well as count, for right now I am just trying to successfully get the distance.
My python script is:
import requests
import json
response_API = requests.get('http://localhost:8998/api/extra/lightning.json')
data = response_API.text
parse_json = json.loads(data)
value = parse_json['Distance to last strike']
print(value)
This does not work. If I change the value line to
value = parse_json['data']
then the entire string I listed above is returned.
I am hoping it's just a simple formatting issue. Suggestions?
You have an object with a list of lists. If you fetch
value = parse_json['data']
Then you will have a list containing three lists. So:
print(value[0][1])
will print "23.0".
This question already has answers here:
Accessing JSON elements
(8 answers)
Closed 8 months ago.
I am struggling trying to extract keys from a JSON output.
requests outputs this JSON:
for (;;);{"__ar":1,"payload":[{"id":"USERID","name":"USERNAME"}]}
at this point I get rid of "for (;;);" in order to get a valid JSON:
json_header = (data.text).replace('for (;;);', '')
Now I need to print USERID and USERNAME. This is what I try:
json_data = json.dumps(json_header)
json_objects = json.loads(json_data)
print(json_objects['payload']['id'])
But I get this:
TypeError: string indices must be integers
Can you help me fix the code?
I am not sure what the relation between json_header and json_data is.
But if your json is
{"__ar":1,"payload":[{"id":"USERID","name":"USERNAME"}]}
so payload is an array of dicts. To access the first (and only) dict in the array you do:
payload = json_objects['payload']
first_elemet = payload[0]
id = first_element['id']
print(id)
or all together, complete and ready to execute:
import json
json_string = '{"__ar":1,"payload":[{"id":"USERID","name":"USERNAME"}]}'
json_objects = json.loads(json_string)
print(json_objects['payload'][0]['id'])
By the way: your original data with the for loop looks scary. If this should be JSON it is strange at least and looks like a bug in whatever backend you are calling.
I tried to use the openweathermap.org rest API inside python. When I tried to assign a key from the dictionary I created with the JSON data this error occurred.
-list indices must be integers or slices, not str
I'm new to python and I couldn't find a solution to this matter.
The code snip I wrote:
import requests
from pprint import pprint
lokka = str(input("What is the location you need information of?"))
#takes the location as "lokka"
hellload = requests.get("http://api.openweathermap.org/data/2.5/weather?q="+ lokka +"&appid=xxxxxxxxxxxxxxxxx&units=metric")
#the rest api's load will be taken to the account of hellload
jputha = hellload.json()
#json data will be converted to a dictionary
#print (jputha)
#---------------------------------------------------------
#from now onward I'll be kickin the hell out the jsons
long = str(jputha["coord"]["lon"])
lat = str(jputha["coord"]["lat"])
wthr = str(jputha["weather"]["main"])
temp = str(jputha["main"]["temp"])
winspd = str(jputha["wind"]["speed"])
print(long)
print(lat)
print(wthr)
print(temp)
print(winspd)
According to OpenWeatherMap's documentation, the JSON response from the API looks like this:
{"coord":
{"lon":145.77,"lat":-16.92},
"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],
"base":"cmc stations",
"main":{"temp":293.25,"pressure":1019,"humidity":83,"temp_min":289.82,"temp_max":295.37},
"wind":{"speed":5.1,"deg":150},
"clouds":{"all":75},
"rain":{"3h":3},
"dt":1435658272,
"sys":{"type":1,"id":8166,"message":0.0166,"country":"AU","sunrise":1435610796,"sunset":1435650870},
"id":2172797,
"name":"Cairns",
"cod":200}
where the weather key contains a list of dicts rather than a dict, so if you simply want the first weather data from the list, you should use [0] to obtain the value of the first index instead:
wthr = str(jputha["weather"][0]["main"])
I have a tuple like: t= ({'count': 5L},)
Here i don't want to use for loop but want to get value as 5.Then how can i do it?
I tried with coverting to string then using JSON.
import json
s = str(t)
d = json.loads(s)
I got error:ValueError: No JSON object could be decoded
And winded up with no result.
I want to get the value of count as integer 5 & store in a variable.
Anyone having any idea?
No need to use Json since it is already your tuple is already a Python data structure.
If you know the index of the item in the tuple, and you know the keyname you can access it directly using:
t = ({'count': 5L},)
value = int(t[0]['count'])