How to parse JSON with a list of lists? - python

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".

Related

I want to convert a list of strings to a list of dictionaries

I stored a twitter data to a mysql db as a json. When I fetch it back, it returns a list of string instead of a list of dictionary. I am looking for a way turn it to a list of dictionaries. This is the format i get the stored data back. "tweetdata" is the column name in the db
[{"tweetdata":"[{\"text\":\"b problem\",\"len\":10,\"Date\":1583160242000,\"Source\":\"Twitter for
Android\",\"Likes\":0,\"RTs\":0},}]"}]
I want it to return something like this as a list of dicts with the column name stripped off
[{\"text\":\"b problem\",\"len\":10,\"Date\":1583160242000,\"Source\":\"Twitter for
Android\",\"Likes\":0,\"RTs\":0},}]
First of all, looks like you provided a wrong json format. Provided that you have a correct json format then you can use json loads function to load the the json data and convert it to dictionary type. Here is code snippet in python.
import json
json_data = '[{"tweetdata":[{\"text\":\"b problem\",\"len\":10,\"Date\":1583160242000,\"Source\":\"Twitter for Android\",\"Likes\":0,\"RTs\":0}]}]'
parsed_json = json.loads(json_data)
parsed_dict = parsed_json[0]['tweetdata'][0]
print(type(parsed_dict))
for item in parsed_dict.items():
print(item)
Above code snippet will print these.
<class 'dict'>
('text', 'b problem')
('len', 10)
('Date', 1583160242000)
('Source', 'Twitter for Android')
('Likes', 0)
('RTs', 0)
Try this:
if your data in tweetdata variable then tweetdata[0]["tweetdata"]
it'll return like this:
[{\"text\":\"b problem\",\"len\":10,\"Date\":1583160242000,\"Source\":\"Twitter for Android\",\"Likes\":0,\"RTs\":0},}]
actually you can do like this:
data = [{"tweetdata":"[{\"text\":\"b problem\",\"len\":10,\"Date\":1583160242000,\"Source\":\"Twitter for Android\",\"Likes\":0,\"RTs\":0},}]"}][0]["tweetdata"]
and print data you'll get the same result.

"list indices must be integers" error, when trying to use keys from a dictionary

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"])

dictionary error - python

Here is my dictionary:
{"draws":{"draw":[{"drawTime":"01-01-2017T22:00:00","drawNo":1771,"results":[3,3,4,9,2,9,1]}]}}
I want to get the "results" from this nested dictionary but an error keeps appearing that the list indices must be integers. Basically I want to get [3,3,4,9,2,9,1]. Any help would be greatly appreciated.
Here is my code:
import urllib2
import json
for dt in range (1,31):
url = 'http://applications.opap.gr/DrawsRestServices/proto/drawDate/%s-01-2017.json'%dt
json_obj = urllib2.urlopen(url)
data = json.load(json_obj)
#num_array = list(data['draws']['draw'])
data1= data['draws']['draw']
print data1['results']
You missed a list.
{"draws":{"draw":[{"drawTime":"01-01-2017T22:00:00","drawNo":1771,"results":[3,3,4,9,2,9,1]}]}}
Look closely at "draw", its value is a list of dicts, so what you need is
data['draws']['draw'][0]['results']

Python - Regex capture multiple fields and build a dictionary with them

I have this in the HTML code:
"name":"London Street" dsakjhasfsa safksafas "north":"232","south":"12","east":"113","west":"9","curRoom":"110"
"name":"London Street" something asdgas dsakhdask "north":"0","south":"22","east":"131","west":"19","curRoom":"10"
I have try those with Regex, but somehow I'm failing somewhere.
\"name\":\"\A(...)\Z\"*?north\":\"(d+)\",\"south\":\"(\d+)\",east\":\"(\d+)\",west\":\"(\d+)\",curRoom\":\"(\d+)\"
\"name\":\"^(...)$\"*?north\":\"(\d+)\",\"south\":\"(\d+)\",east\":\"(\d+)\",west\":\"(\d+)\",currentRoom\":\"(d+)\"
\"name\":\"(...)+\"*?north\":\"(\d+)\",\"south\":\"(d+)\",east\":\"(d+)\",west\":\"(\d+)\",currentRoom\":\"(\d+)\"
And with those captures I want to create a dictionary like this:
{ key is current room, values [position 0: a list with neighbours [1,2,3], position 1 - the name of the room] }
I only know how to achieve this until a point, by assigning to variable the find for each room like this:
list_of_neighbours = []
number_south = re.findall('south\":\"(\d+)\"', url)
list_of_neighbours.append(number_south)
....
list_of_neighbours = [n,s,e,w]
dictionay ={}
for k, v in list_of_neighbours:
dictionay[k] = str(number_current_room)
dictionay[k].append(v)
but this doesnt add the room and have too many steps.
The questions are: Its possible to be a shorter version? and How can I fix the regex find?Thanks
You're trying to parse JSON with regex. Don't. JSON is basically already a dictionary, just stringified in a standard format.
Use the json module:
import json
rooms = json.loads(some_json_string)

Urlencode dictionary using Python - naming key and value in the url

I am attempting to generate a URL link in the following format using urllib and urlencode.
<img src=page.psp?KEY=%28SpecA%2CSpecB%29&VALUE=1&KEY=%28SpecA%2C%28SpecB%2CSpecC%29%29&VALUE=2>
I'm trying to use data from my dictionary to input into the urllib.urlencode() function however, I need to get it into a format where the keys and values have a variable name, like below. So the keys from my dictionary will = NODE and values will = VALUE.
wanted = urllib.urlencode( [("KEY",v1),("VALUE",v2)] )
req.write( "<a href=page.psp?%s>" % (s) );
The problem I am having is that I want the URL as above and instead I am getting what is below, rather than KEY=(SpecA,SpecB) NODE=1, KEY=(SpecA,SpecB,SpecC) NODE=2 which is what I want.
KEY=%28SpecA%2CSpecB%29%2C%28%28SpecA%2CSpecB%29%2CSpecC%29&VALUE=1%2C2
So far I have extracted keys and values from the dictionary, extracted into tuples, lists, strings and also tried dict.items() but it hasn't helped much as I still can't get it to go into the format I want. Also I am doing this using Python server pages which is why I keep having to print things as a string due to constant string errors. This is part of what I have so far:
k = (str(dict))
ver1 = dict.keys()
ver2 = dict.values()
new = urllib.urlencode(function)
f = urllib.urlopen("page.psp?%s" % new)
I am wondering what I need to change in terms of extracting values from the dictionary/converting them to different formats in order to get the output I want? Any help would be appreciated and I can add more of my code (as messy as it has become) if need be. Thanks.
This should give you the format you want:
data = {
'(SpecA,SpecB)': 1,
'(SpecA,SpecB,SpecC)': 2,
}
params = []
for k,v in data.iteritems():
params.append(('KEY', k))
params.append(('VALUE', v))
new = urllib.urlencode(params)
Note that the KEY/VALUE pairings may not be the order you want, given that dicts are unordered.

Categories

Resources