Related
I'm trying to find full path to every key in my json
{ "Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder":{
"CoopRoomDifficultySetups": [
{
"RoomDifficulties": [
{
"Id" : 510,
"IsEnabled" : 1,
"AvailableRegions" : [ ],
"Weight" : 1,
"MinLevel" : 60,
"MaxLevel" : 69,
"MinFriendsLevel" : 50,
"MaxFriendsLevel" : 99,
"MinPunishLevel" : 0,
"MaxPunishLevel" : 0,
"MinHardCP" : "0",
"MinCP" : "0",
"MaxCP" : "0",
"MaxHardCP" : "0",
"LowPowerBonus" : 0.5,
"LowPowerCp" : "0",
"LowPowerLevel" : 90,
"Maps" : [ {
"MapId" : 4,
"NpcPreset" : "NPCPresetMap5Coop3",
"TypesLimit" : 1000,
"TierSpawnProbabilities" : [ {
"Tier" : 0,
"SpawnProbability" : 0.6
}, {
"Tier" : 1,
"SpawnProbability" : 0.75
}, {
"Tier" : 2,
"SpawnProbability" : 0.52
}, {
"Tier" : 3,
"SpawnProbability" : 0.6
} ],
"ChampionProbabilityTier2" : 0.1,
"ChampionProbabilityTier3" : 0.08,
"PlayersWhenMaxProbabilityTier2" : 3,
"PlayersWhenMaxProbabilityTier3" : 6,
"NpcLevelMultiplier" : 1.15,
"MapWeight" : 1,
"PointsNpcMultiplier" : 0.85,
"XpNpcMultiplier" : 0.85,
"ScoreNpcMultiplier" : 0.85,
"NpcMinLevel" : 63,
"NpcMaxLevel" : 77
} ],
"TimeOfDayMode_Parsable" : 0
}]},[{"foo":"foo"}]]}}
And with that being said I've found a function on stackoverflow to do it, however it doesn't return the exact path that I need to walk through manually to access the values, for example: to access "Id":
json['Assets']['Coop'][0]['Room'][0]['Id'] and then it returns 510
however this function returns the following path:
json['Assets']['Coop']['Room']['Id']
So it looks as if it doesn't read lists like I'd like it to. What's more I've already tried deepmerge library as a solution since my main goal is to read all the values from the json above and then compare it with another json, and when it finds "Id" : 510, then all the values below should be changed
def walktree(tree, at=lambda node: not isinstance(node, dict), prefix=(),
flattennode=lambda node:isinstance(node, (list, tuple, set))):
"""
Traverse a tree, and return a iterator of the paths from the root nodes to the leaf nodes.
tree: like '{'a':{'b':1,'c':2}}'
at: a bool function or a int indicates levels num to go down. walktree(tree, at=1) equivalent to tree.items()
flattennode: a bool function to decide whether to iterate at node value
"""
if isinstance(at, int):
isleaf_ = at == 0
isleaf = lambda v: isleaf_
at = at - 1
else:
isleaf = at
if isleaf(tree):
if not flattennode(tree):
yield (*prefix, tree)
else:
for v in tree:
yield from walktree(v, at, prefix, flattennode=flattennode)
else:
for k,v in tree.items():
yield from walktree(v, at, (*prefix, k), flattennode=flattennode)
Here is a generator which meets your requirements:
def dict_generator(dictionary, previous=None):
previous = previous[:] if previous else []
if isinstance(dictionary, dict):
for key, value in dictionary.items():
if isinstance(value, dict):
for d in dict_generator(value, previous + [key]):
yield d
elif isinstance(value, list) or isinstance(value, tuple):
for k,v in enumerate(value):
for d in dict_generator(v, previous + [key] + [[k]]):
yield d
else:
yield previous + [key, value]
else:
yield previous + [dictionary]
mydict ={'foo': {'foo1': [1,2,3], 'foo2': 1}}
print(list(dict_generator(mydict)))
Will produce output like this:
[['foo', 'foo1', [0], 1], ['foo', 'foo1', [1], 2], ['foo', 'foo1', [2], 3], ['foo', 'foo2', 1]]
Preparation:
jsonpath-ng can parse even such a nested json object very easily. It can be installed by the following command:
pip install --upgrade jsonpath-ng
Code:
import jsonpath_ng as jp
# Create the sample json
data = {"Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder":{ "CoopRoomDifficultySetups": [ { "RoomDifficulties": [ { "Id" : 510, "IsEnabled" : 1, "AvailableRegions" : [ ], "Weight" : 1, "MinLevel" : 60, "MaxLevel" : 69, "MinFriendsLevel" : 50, "MaxFriendsLevel" : 99, "MinPunishLevel" : 0, "MaxPunishLevel" : 0, "MinHardCP" : "0", "MinCP" : "0", "MaxCP" : "0", "MaxHardCP" : "0", "LowPowerBonus" : 0.5, "LowPowerCp" : "0", "LowPowerLevel" : 90, "Maps" : [ { "MapId" : 4, "NpcPreset" : "NPCPresetMap5Coop3", "TypesLimit" : 1000, "TierSpawnProbabilities" : [ { "Tier" : 0, "SpawnProbability" : 0.6 }, { "Tier" : 1, "SpawnProbability" : 0.75 }, { "Tier" : 2, "SpawnProbability" : 0.52 }, { "Tier" : 3, "SpawnProbability" : 0.6 } ], "ChampionProbabilityTier2" : 0.1, "ChampionProbabilityTier3" : 0.08, "PlayersWhenMaxProbabilityTier2" : 3, "PlayersWhenMaxProbabilityTier3" : 6, "NpcLevelMultiplier" : 1.15, "MapWeight" : 1, "PointsNpcMultiplier" : 0.85, "XpNpcMultiplier" : 0.85, "ScoreNpcMultiplier" : 0.85, "NpcMinLevel" : 63, "NpcMaxLevel" : 77 } ], "TimeOfDayMode_Parsable" : 0 }]},[{"foo":"foo"}]]}}
# Define a dictionary
d = {}
# Define an expression to parse the json object
expr = jp.parse('$..*')
for m in expr.find(data):
# Show a json path
print(str(m.full_path))
# Update the dictionary
d[str(m.full_path)] = m.value
# Show an example value in the dictionary
key = 'Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].Maps.[0].NpcMaxLevel'
print(d[key])
Output:
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].Id
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].IsEnabled
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].AvailableRegions
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].Weight
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].MinLevel
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].MaxLevel
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].MinFriendsLevel
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].MaxFriendsLevel
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].MinPunishLevel
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].MaxPunishLevel
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].MinHardCP
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].MinCP
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].MaxCP
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].MaxHardCP
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].LowPowerBonus
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].LowPowerCp
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].LowPowerLevel
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].Maps
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].TimeOfDayMode_Parsable
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].Maps.[0].MapId
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].Maps.[0].NpcPreset
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].Maps.[0].TypesLimit
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].Maps.[0].TierSpawnProbabilities
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].Maps.[0].ChampionProbabilityTier2
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].Maps.[0].ChampionProbabilityTier3
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].Maps.[0].PlayersWhenMaxProbabilityTier2
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].Maps.[0].PlayersWhenMaxProbabilityTier3
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].Maps.[0].NpcLevelMultiplier
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].Maps.[0].MapWeight
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].Maps.[0].PointsNpcMultiplier
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].Maps.[0].XpNpcMultiplier
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].Maps.[0].ScoreNpcMultiplier
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].Maps.[0].NpcMinLevel
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].Maps.[0].NpcMaxLevel
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].Maps.[0].TierSpawnProbabilities.[0].Tier
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].Maps.[0].TierSpawnProbabilities.[0].SpawnProbability
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].Maps.[0].TierSpawnProbabilities.[1].Tier
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].Maps.[0].TierSpawnProbabilities.[1].SpawnProbability
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].Maps.[0].TierSpawnProbabilities.[2].Tier
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].Maps.[0].TierSpawnProbabilities.[2].SpawnProbability
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].Maps.[0].TierSpawnProbabilities.[3].Tier
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[0].RoomDifficulties.[0].Maps.[0].TierSpawnProbabilities.[3].SpawnProbability
Assets/CustomGameData/Resources/Configs/RoomDifficulty/RoomDifficultySetupHolder.CoopRoomDifficultySetups.[1].[0].foo
and
77
What almost helped me completely is that piece of code
https://stackoverflow.com/a/59186999/18018783
def dict_generator(indict, pre=None):
pre = pre[:] if pre else []
if isinstance(indict, dict):
for key, value in indict.items():
if isinstance(value, dict):
for d in dict_generator(value, pre + [key]):
yield d
elif isinstance(value, list) or isinstance(value, tuple):
for k,v in enumerate(value):
for d in dict_generator(v, pre + [key] + [k]):
yield d
else:
yield pre + [key, value]
else:
yield indict
However still there is one exception when it doesn't work as I wish for example when this structure is given
{
"foo":{
"foo1": [1,2,3],
"foo2": 1
}
}
the output of dict_generator will be
[1, 2, 3, ['foo', 'foo2', 1]]
so it doesn't give me a path to the key that has a list as an value since my desired output should look that way:
[['foo', 'foo1', [0], 1], ['foo', 'foo2', [1], 2], ['foo', 'foo2, [2], 3], ['foo', 'foo2', 1]]]
I've exported fitbit sleep data and got a json file with nested variables + dict. I would like to convert the json file to a csv file that will display all "regular" variables, e.g. "dateOfSleep" but also the nested variables, e.g. "deep" & "wake" with all dictionary information.
I tried json_normalize; but I can only make it work for the first nested variables, e.g. "levels". Anybody has an idea?
Much appreciated.
[{
"logId" : 32072056107,
"dateOfSleep" : "2021-05-08",
"startTime" : "2021-05-07T23:22:00.000",
"endTime" : "2021-05-08T08:05:30.000",
"duration" : 31380000,
"minutesToFallAsleep" : 0,
"minutesAsleep" : 468,
"minutesAwake" : 55,
"minutesAfterWakeup" : 0,
"timeInBed" : 523,
"efficiency" : 93,
"type" : "stages",
"infoCode" : 0,
"levels" : {
"summary" : {
"deep" : {
"count" : 5,
"minutes" : 85,
"thirtyDayAvgMinutes" : 68
},
"wake" : {
"count" : 30,
"minutes" : 55,
"thirtyDayAvgMinutes" : 56
},
"light" : {
"count" : 30,
"minutes" : 267,
"thirtyDayAvgMinutes" : 235
},
"rem" : {
"count" : 10,
"minutes" : 116,
"thirtyDayAvgMinutes" : 94
}
},
.....
Use pd.json_normalize, all nested levels are flatten and join with dots (by default):
import pandas as pd
import json
with open('data.json') as fp:
data = json.load(fp)
df = pd.json_normalize(data)
Output:
>>> df
logId dateOfSleep startTime ... levels.summary.rem.count levels.summary.rem.minutes levels.summary.rem.thirtyDayAvgMinutes
0 32072056107 2021-05-08 2021-05-07T23:22:00.000 ... 10 116 94
[1 rows x 25 columns]
Content of data.json:
[{
"logId" : 32072056107,
"dateOfSleep" : "2021-05-08",
"startTime" : "2021-05-07T23:22:00.000",
"endTime" : "2021-05-08T08:05:30.000",
"duration" : 31380000,
"minutesToFallAsleep" : 0,
"minutesAsleep" : 468,
"minutesAwake" : 55,
"minutesAfterWakeup" : 0,
"timeInBed" : 523,
"efficiency" : 93,
"type" : "stages",
"infoCode" : 0,
"levels" : {
"summary" : {
"deep" : {
"count" : 5,
"minutes" : 85,
"thirtyDayAvgMinutes" : 68
},
"wake" : {
"count" : 30,
"minutes" : 55,
"thirtyDayAvgMinutes" : 56
},
"light" : {
"count" : 30,
"minutes" : 267,
"thirtyDayAvgMinutes" : 235
},
"rem" : {
"count" : 10,
"minutes" : 116,
"thirtyDayAvgMinutes" : 94
}
}
}
}]
I have the following dictionary from which I want to get the 3 maximum of the last value.
from the three. How should I go about it using a loop?
G0001NB : ['Fredericton Gardens', '264.813134990644', 23833.18214915796]
G0002NB : ['Saint John Flowerbeds', '308.65432870469186', 27778.889583422268]
G0001NS : ['Halifax Gardens', '318.94189777177445', 28704.7707994597]
G0001PE : ['Charlottetown Gardens', '361.21898123683434', 32509.70831131509]
G0002PE : ['Summerside Meadows', '264.95778233234654', 23846.200409911187]
G0001NL : ['St.Johns Flowers', '524.3613531477152', 47192.521783294374]
G0002NL : ['Flowers in Labrador', '400.6868146749942', 36061.813320749476]
G0001QC : ['Fleur de Montreal', '261.0589768948249', 23495.307920534244]
G0001ON : ['Ottawa Fields', '325.5476915528243', 29299.292239754184]
G0002ON : ['Toronto Meadows', '275.4492991562006', 24790.436924058053]
G0003ON : ['London Gardens', '301.3513495675443', 27121.621461078983]
G0001MB : ['Winnipeg Meadows', '117.43417931323137', 10569.076138190823]
G0001SK : ['Regina Gardens', '307.6230292454801', 27686.072632093208]
G0002SK : ['Saskatoon Gardens', '354.3851315502206', 31894.661839519853]
G0001AB : ['Calgary Fields', '141.08265897062734', 12697.43930735646]
G0002AB : ['Edmonton Meadows', '351.50216826137427', 31635.195143523684]
G0001BC : ['Vancouver Meadows', '332.59825321044787', 29933.842788940306]
G0002BC : ['Victoria Gardens', '173.34623973921785', 15601.161576529606]
G0001YT : ['Whitehorse Fields', '355.09873981736985', 31958.886583563286]
G0001NT : ['Yellowknife Plot', '149.09626155613265', 13418.663540051939]
G0001NU : ['Iqaluit Greenhouse', '291.6230202078498', 26246.07181870648]
try something like this:
dictionary = {False: [1, 2, 10],
10: ["", 2, 20],
1: [1, "", 30],
20: [1, "", 1]}
values = [i[-1] for i in dictionary.values()]
values.sort(reverse=True)
output = sum(values[:3])
Output: 60
I got it working.
I first sorted the list with list.sort()
and then printed the list[-n:]
I am trying to create dictionaries that will be order requests for a portfolio of funds. The dictionaries will be exported as JSON files, just to give a bit of context. This is what the dictionaries look like.
{
"accountID":"02e57c7d-d071-4c63-b491-1194a9939ea5.1452548617456",
"accountNo":"DWTE000005",
"userID":"02e57c7d-d071-4c63-b491-1194a9939ea5",
"accountType":2,
"ordType":"1",
"side":"B",
"instrumentID":"06926627-e950-48f3-9c53-b679f61120ec",
"orderQty":1.4312,
"comment":""
}
The condition that the dictionaries will be created upon are if the target weightings are above 0, as follows.
targetportfolio = {
'VXUS' : 0.2,
'GOVT' : 0,
'IVW' : 0.2,
'EEM' : 0.2,
'JNK' : 0,
'VDE' : 0.15,
'LQD' : 0,
'IJR' : 0.1,
'BIL' : 0,
'AAXJ' : 0.15
}
Where each fund (for example VXUS) will have an order generated if the amount in the target portfolio dict is greater than 0. I was thinking of something along the lines of the code below, but I am quite confused. Any ideas?
def ordergen(portfolio):
for i in portfolio:
{
"accountID" : "02e57c7d-d071-4c63-b491-1194a9939ea5.1452548617456",
"accountNo" : "DWTE000005",
"userID" : "02e57c7d-d071-4c63-b491-1194a9939ea5",
"accountType" : 2,
"ordType" : "1",
"side" : "B",
"instrumentID" :fundID[i],
"orderQty" : ,
"comment":""
}
targetPortfolio = {
'VXUS' : 0.2,
'GOVT' : 0,
'IVW' : 0.2,
'EEM' : 0.2,
'JNK' : 0,
'VDE' : 0.15,
'LQD' : 0,
'IJR' : 0.1,
'BIL' : 0,
'AAXJ' : 0.15
}
orders = []
for k in targetPortfolio.keys():
if targetPortfolio[k] > 0:
orders.append({
"accountID" : "02e57c7d-d071-4c63-b491-1194a9939ea5.1452548617456",
"accountNo" : "DWTE000005",
"userID" : "02e57c7d-d071-4c63-b491-1194a9939ea5",
"accountType" : 2,
"ordType" : "1",
"side" : "B",
"instrumentID" : "",
"orderQty" : "",
"comment":""
})
print(orders)
.keys() is your friend. You can now use 'k' to fill in your order details under the condition that their weight is above 0.
I want to get weather data in spotfire as a html data table. Spotfire does provide Ironpython support but not the flexibility to add our own modules. So being a shorthand here. Using iframe and src as api doesnt help in this case.
My Script:
import clr
clr.AddReference('System.Data')
clr.AddReference('System.Web.Extensions')
import System
from System import DateTime
from System.Data import DataSet, DataTable
from System.IO import StreamReader, StreamWriter, MemoryStream, SeekOrigin
from System.Net import HttpWebRequest
from System.Web.Script.Serialization import JavaScriptSerializer
from Spotfire.Dxp.Data import DataType, DataTableSaveSettings
from Spotfire.Dxp.Data.Import import TextFileDataSource, TextDataReaderSettings
#uri = "http://api.openweathermap.org/data/2.5/weather?q=London&appid=ec0313a918fa729d4372555ada5fb1f8"
uri = "http://api.openweathermap.org/data/2.5/group?id=524901,703448,2643743&units=metric&appid=ec0313a918fa729d4372555ada5fb1f8"
webRequest = HttpWebRequest.Create(uri)
response = webRequest.GetResponse()
streamReader = StreamReader(response.GetResponseStream())
jsonData = streamReader.ReadToEnd()
js = JavaScriptSerializer()
dataDict = js.Deserialize(jsonData, object)
print dataDict
# Close the connection
response.Close()
I need to extract some common values for multiple cities like: Name, Id, Description, temperature from the dictionary dataDict.
I am hitting a stone here.
It would be great if you can help me/ guide me on how to get these values from this complex dictionary.
Example dataDict: Dictionary[str, object]({'cnt' : 3, 'list' : Array[object]((Dictionary[str, object]({'coord' : Dictionary[str, object]({'lon' : <System.Decimal object at 0x0000000000000055 [37.62]>, 'lat' : <System.Decimal object at 0x0000000000000056 [55.75]>}), 'sys' : Dictionary[str, object]({'type' : 1, 'id' : 7323, 'message' : <System.Decimal object at 0x0000000000000057 [0.2107]>, 'country' : 'RU', 'sunrise' : 1484372967, 'sunset' : 1484400490}), 'weather' : Array[object]((Dictionary[str, object]({'id' : 802, 'main' : 'Clouds', 'description' : 'scattered clouds', 'icon' : '03d'}))), 'main' : Dictionary[str, object]({'temp' : <System.Decimal object at 0x0000000000000058 [-1.5]>, 'pressure' : 1009, 'humidity' : 80, 'temp_min' : -2, 'temp_max' : -1}), 'visibility' : 10000, 'wind' : Dictionary[str, object]({'speed' : 6, 'deg' : 160, 'gust' : 12}), 'clouds' : Dictionary[str, object]({'all' : 40}), 'dt' : 1484398800, 'id' : 524901, 'name' : 'Moscow'}), Dictionary[str, object]({'coord' : Dictionary[str, object]({'lon' : <System.Decimal object at 0x0000000000000059 [30.52]>, 'lat' : <System.Decimal object at 0x000000000000005A [50.43]>}), 'sys' : Dictionary[str, object]({'type' : 1, 'id' : 7358, 'message' : <System.Decimal object at 0x000000000000005B [0.1886]>, 'country' : 'UA', 'sunrise' : 1484373141, 'sunset' : 1484403724}), 'weather' : Array[object]((Dictionary[str, object]({'id' : 804, 'main' : 'Clouds', 'description' : 'overcast clouds', 'icon' : '04d'}))), 'main' : Dictionary[str, object]({'temp' : 3, 'pressure' : 998, 'humidity' : 86, 'temp_min' : 3, 'temp_max' : 3}), 'visibility' : 10000, 'wind' : Dictionary[str, object]({'speed' : 4, 'deg' : 170, 'var_beg' : 100, 'var_end' : 210}), 'clouds' : Dictionary[str, object]({'all' : 90}), 'dt' : 1484398800, 'id' : 703448, 'name' : 'Kiev'}), Dictionary[str, object]({'coord' : Dictionary[str, object]({'lon' : <System.Decimal object at 0x000000000000005C [-0.13]>, 'lat' : <System.Decimal object at 0x000000000000005D [51.51]>}), 'sys' : Dictionary[str, object]({'type' : 1, 'id' : 5091, 'message' : <System.Decimal object at 0x000000000000005E [0.1699]>, 'country' : 'GB', 'sunrise' : 1484380764, 'sunset' : 1484410813}), 'weather' : Array[object]((Dictionary[str, object]({'id' : 501, 'main' : 'Rain', 'description' : 'moderate rain', 'icon' : '10d'}))), 'main' : Dictionary[str, object]({'temp' : <System.Decimal object at 0x000000000000005F [4.01]>, 'pressure' : 1020, 'humidity' : 80, 'temp_min' : 2, 'temp_max' : 6}), 'visibility' : 10000, 'wind' : Dictionary[str, object]({'speed' : <System.Decimal object at 0x0000000000000060 [5.7]>, 'deg' : 290}), 'clouds' : Dictionary[str, object]({'all' : 20}), 'dt' : 1484400000, 'id' : 2643743, 'name' : 'London'})))})