I am attempting to create a dictionary by looping through a weather JSON file.
I want the key to be date and the value to be temperature from the nested "hours" in the JSON.
combined_temperature_hour = {date, temperature}
As I loop through the JSON file I can get the individual items but I can't figure out how to grab the two values and put them together in a (key, value) pair.
combined_temperature_hour = {}
for d in days:
for hours in d["hours"]:
hourly = hours.get('datetimeEpoch')
dia_y_hora = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(hourly))
complete_date.append(dia_y_hora)
h_temp = hours.get('temp')
hourly_temperature.append(h_temp)
#combined
combined = {complete_date, hourly_temperature}
print(combined)
JSON
"days":[
{
"datetime":"2021-01-01",
"datetimeEpoch":1609484400,
"tempmax":36.9,
"tempmin":32.4,
"temp":34.7,
"hours":[
{
"datetime":"00:00:00",
"datetimeEpoch":1609484400,
"temp":32.9,
},
{
"datetime":"01:00:00",
"datetimeEpoch":1609488000,
"temp":33.2,
},
{
"datetime":"02:00:00",
"datetimeEpoch":1609491600,
"temp":33.2,
}
]
}
This is what I would like the dictionary to look like after the loop
combined_temperature_hour = {`2021-01-24 05:00:00`: '25.1', `2021-01-24 06:00:00`: '26.2'}
I have tried grabbing the list for temperature and hour and combining into a dictionary them after the loop but I noticed that I am missing values and they are probably incorrect pairs.
Read your json as a dictionary first into a variable a
(I changed some of the mismatching brackets in your json)
a = {"days":[
{
"datetime":"2021-01-01",
"datetimeEpoch":1609484400,
"tempmax":36.9,
"tempmin":32.4,
"temp":34.7,
"hours":[
{
"datetime":"00:00:00",
"datetimeEpoch":1609484400,
"temp":32.9,
},
{
"datetime":"01:00:00",
"datetimeEpoch":1609488000,
"temp":33.2,
},
{
"datetime":"02:00:00",
"datetimeEpoch":1609491600,
"temp":33.2,
}
]
}
]
}
Then you can have the expected output from the following code:
combined_temperature_hour = {}
for date in a['days']:
for hour in date['hours']:
combined_temperature_hour[date['datetime']+' '+hour['datetime']] = hour['temp']
Related
can someone explain how I convert the following json into a simple data frame with the following headings?
----- sample----
{
"last_scanned_block": 14968718,
"blocks": {
"13965799": {
"0x9603846aff5c425277e483de16179a68dbc739debcc5449ea99e45c9d0924430": {
"165": {
"from": "0x0000000000000000000000000000000000000000",
"to": "0x01f87c337be5636Cd9B3D48F1159768A7e7837A5",
"value": 100000000000000000000000000,
"timestamp": "2022-01-08T16:19:02"
}
}
},
"13965820": {
"0xd4a4122734a522c40504c8b0ab43b9aa40ac821cd9913179b3ae64e5b166fc57": {
"226": {
"from": "0x01f87c337be5636Cd9B3D48F1159768A7e7837A5",
"to": "0xEa3Fa123Eb40CEEaeED390D8d6dE6AF95f044AF7",
"value": 610000000000000000000000,
"timestamp": "2022-01-08T16:25:12"
}
}
},
--- end----
I'd like the df to have the following 8 column headings and values for each row
(value examples for first row)
Last_scanned_block: 14968718
block: 13965799
hex: 0x9603846aff5c425277e483de16179a68dbc739debcc5449ea99e45c9d0924430
number: 165
from: 0x0000000000000000000000000000000000000000
to: 0x01f87c337be5636Cd9B3D48F1159768A7e7837A5
value: 100000000000000000000000000
timestamp: 2022-01-08T16:19:02
Thanks
I would make a new dictionary from the json that is passed in. Essentially instead of having nested dictionaries like you have above you want to get them into one simple dictionary according to your headings and values. It should be:
*heading name* : *list of values*
Essentially, the resulting format should be:
{"Last_scanned_block" : [14968718], "block" : [13965799], "hex" : ["0x9603846aff5c425277e483de16179a68dbc739debcc5449ea99e45c9d0924430"], "number" : [165], "from" : ["0x0000000000000000000000000000000000000000"], "to" : ["0x01f87c337be5636Cd9B3D48F1159768A7e7837A5"], "value": [100000000000000000000000000], "timestamp" : ["2022-01-08T16:19:02"]}
Then every time you read more data you just append it to each respective list in your dictionary.
Once you have your complete dictionary, you would use pandas. So something along the lines of:
import pandas
d = *the dictionary above*
frame = pandas.DataFrame(data = d)
print(frame)
JSON output:
"machines": {
"machines-xyz-123": {
"vm": {
"id": "compute1234",
"createTimestamp": "2020-11-15T14:44:02.272908941Z",
"status": "running"
}
"machines-tyu-567": {
"vm": {
"id": "compute23456",
"createTimestamp": "2020-11-15T18:18:22.412021105Z",
"status": "running"
}
}
}
Machine ID changes dynamically each time, I am able to get the VM ID and timestamp of each machine with below code:
machine_list = json_output[machines]
key_list = list(machines.keys())
count = len(machine_list)
for i in range (count):
id = json_output['machines'][key_list[i]]['vm']['id']
date = json_output['machines'][key_list[i]]['vm']['creationTimestamp']
I need to do 2 operations from the JSON output further:
1.) Need to form a new JSON dictionary with 'id' value as key and
'timestamp' value as value of the id key
2.) Need to retrieve the id based on the latest timestamp value from
the JSON dictionary
Any help will be appreciated.
not sure i understand your question
import json
my_dict = {}
my_dict[id] = timestamp
my_json = json.dumps(my_dict) #to create a json and save it as a file
out_file = open(my_path,"w")
out_file.write(my_json)
out_file.close()
#to compare between dates see https://stackoverflow.com/questions/8142364/how-to-compare-two-dates
max_date = 0
max_id = 0
for id in my_dict:
if my_dict[id]>max_date: # use date compare method
max_date = my_dict[id]
max_id = id
Below is my sample json. Am trying to extract "attributes" part of the json and insert into a relational database. But I needed to construct "name" values as relational columns and insert "value" values into table. I mean
{"name":"ID","value":"528BE6D9FD"} "ID" as a column and insert 528BE6D9FD under the "ID". Its just beginning of my python learning so not sure on how to construct columns from dictionary values.
d = 'C:/adapters/sample1.json'
json_data = open(d).read()
json_file = json.loads(json_data)
for children in json_file["events"]:
#print (children)
for grandchildren in children["attributes"]:
#print(grandchildren)
for key, value in grandchildren.iteritems():
#if key == 'name':
print value
{
"events":[
{
"timestamp":"2010-11-20T11:08:00.978Z",
"code":"Event",
"namespace":null,
"version":null,
"attributes":[
{
"name":"ID",
"value":"528BE6D9FD"
},
{
"name":"Total",
"value":67
},
{
"name":"PostalCode",
"value":"6064"
},
{
"name":"Category",
"value":"More"
},
{
"name":"State",
"value":"QL"
},
{
"name":"orderDateTime",
"value":"2010-07-20T12:08:13Z"
},
{
"name":"CategoryID",
"value":"1091"
},
{
"name":"billingCountry",
"value":"US"
},
{
"name":"shipping",
"value":"Go"
},
{
"name":"orderFee",
"value":77
},
{
"name":"Name",
"value":"Roy"
}
]
}
]
}
As far as extracting the attributes hash of your json data, I would do that like so:
json_path = "c:\\adapters\\sample1.json"
with open(json_path) as json_file:
json_dict = json.load(json_file)
attributes = json_dict['events'][0]['attributes']
Now, I don't know which database system you are using, but regardless, you can extract names, and values with list comprehensions like so:
names = [key['name'] for key in attributes]
values = [key['value'] for key in attributes]
And now just create a table if needed, insert names as column headers, and insert values as a single row with respect to names.
I have such slice of loaded json tp python dictionary (size_dict):
{
"sizeOptionName":"XS",
"sizeOptionId":"1528",
"sortOrderNumber":"7017"
},
{
"sizeOptionName":"S",
"sizeOptionId":"1529",
"sortOrderNumber":"7047"
},
{
"sizeOptionName":"M",
"sizeOptionId":"1530",
"sortOrderNumber":"7095"
}
and I have products with size Id (dictionary_prod):
{
"catalogItemId":"7627712",
"catalogItemTypeId":"3",
"regularPrice":"0.0",
"sizeDimension1Id":"1528",
"sizeDimension2Id":"0",
}
I need to make such as output for any product:
result_dict = {'variant':
[{"catalogItemId":"7627712", ...some other info...,
'sizeName': 'XS', 'sizeId': '1525'}}]}
so I need to convert size ID and add it to new result object
What is the best pythonic way to do this?
I dont know how to get right data from size_dict
if int(dictionary_prod['sizeDimension1Id']) > o:
(result_dict['variant']).append('sizeName': size_dict???)
As Tommy mentioned, this is best facilitated by mapping the size id's to their respective dictionaries.
size_dict = \
[
{
"sizeOptionName":"XS",
"sizeOptionId":"1528",
"sortOrderNumber":"7017"
},
{
"sizeOptionName":"S",
"sizeOptionId":"1529",
"sortOrderNumber":"7047"
},
{
"sizeOptionName":"M",
"sizeOptionId":"1530",
"sortOrderNumber":"7095"
}
]
size_id_map = {size["sizeOptionId"] : size for size in size_dict}
production_dict = \
[
{
"catalogItemId":"7627712",
"catalogItemTypeId":"3",
"regularPrice":"0.0",
"sizeDimension1Id":"1528",
"sizeDimension2Id":"0",
}
]
def make_variant(idict):
odict = idict.copy()
size_id = odict.pop("sizeDimension1Id")
odict.pop("sizeDimension2Id")
odict["sizeName"] = size_id_map[size_id]["sizeOptionName"]
odict["sizeId"] = size_id
return odict
result_dict = \
{
"variant" : [make_variant(product) for product in production_dict]
}
print(result_dict)
Your question is a little confusing but it looks like you have a list (size_dict) of dictionaries that contain some infroamtion and you want to do a lookup to find a particular element in the list that contains the SizeOptionName you are interested in so that you can read off the SizeOptionID.
So first you could organsie your size_dict as a dictionary rather than a list - i.e.
sizeDict = {"XS":{
"sizeOptionName":"XS",
"sizeOptionId":"1528",
"sortOrderNumber":"7017"
}, "S": {
"sizeOptionName":"S",
"sizeOptionId":"1529",
"sortOrderNumber":"7047"
}, ...
You could then read off the SizeOptionID you need by doing:
sizeDict[sizeNameYouAreLookingFor][SizeOptionID]
Alternative you could keep your current structure and just search the list of dictionaries that is size_dict.
So:
for elem in size_dict:
if elem.SizeOptionID == sizeYouAreLookingFor:
OptionID = elem.SizeOptionId
Or perhaps you are asking something else?
I can't figure out how to loop though a JSON object that is deeper than 1 level. The object is:
{
"data":[
{
"id":"251228454889939/insights/page_fan_adds_unique/day",
"name":"page_fan_adds_unique",
"period":"day",
"values":[
{
"value":9,
"end_time":"2012-05-29T07:00:00+0000"
},
{
"value":5,
"end_time":"2012-05-30T07:00:00+0000"
}
],
"title":"Daily New Likes",
"description":"Daily The number of new people who have liked your Page (Unique Users)"
},
{
"id":"251228454889939/insights/page_fan_adds/day",
"name":"page_fan_adds",
"period":"day",
"values":[
{
"value":9,
"end_time":"2012-05-29T07:00:00+0000"
},
{
"value":5,
"end_time":"2012-05-30T07:00:00+0000"
}
],
"title":"Daily New Likes",
"description":"Daily The number of new people who have liked your Page (Total Count)"
}
]
}
Code:
def parseJsonData(data):
output_json = json.loads(data)
for i in output_json:
print i
for k in output_json[i]:
print k
How come I can't access the object like: output_json[data][id]?
I get an error if I try this:
string indice must be an integer
Being that your "data" key is actually a list of objects, you cannot access the items by their "id" field directly. You would need to access each item by a list index such as:
output_json["data"][0]["id"]
Now, if what you want to do is to be able to index the members of "data" by the "id" field as the key, you could reformat your data:
# make "data" a dict {id: item, }, instead of list [item1, item2, ...]
output_json['data'] = dict((item['id'], item) for item in json_data['data'])
print output_json['data']
# {'251228454889939/insights/page_fan_adds_unique/day': ...
print output_json['data']['251228454889939/insights/page_fan_adds_unique/day']
# {'description': 'Daily The number of new p ...
# ways to loop over "data"
for id_, item in output_json['data'].iteritems():
print id_, item
for item in output_json['data'].itervalues():
print item
Otherwise what you have to do is just loop over "data", since there is no real correlation between the index and the object:
for item in output_json["data"]:
print item['id']
# 251228454889939/insights/page_fan_adds_unique/day
# 251228454889939/insights/page_fan_adds/day
What you pasted is not valid JSON. There is an unmatched [ after "data".
Based on this, I would guess that maybe the data isn't what you think it is. If the value of output_json[data] is a list, then you won't be able to access output_json[data][id]. Instead you'll have to do something like output_json[data][0][id], where the [0] accesses the first item in the list.