I want to save the information from a .json file as a dictionary containing other dictionaries. I attempted, but, when I try to access the first key, it is a string, rather than another dictionary. Here is my code:
with open('matches1.json', 'r') as json_file:
match_histories = json.load(json_file)
print(match_histories[key]['matches'])
for i in range(6):
print(match_histories[key][i])
The first print results in an error, the second results in 'matches'.
The file I want to load can be downloaded but the structure is basically:
{
"matches": [
{
"matchId": 1778839570,
"region": "NA",
"platformId": "NA1",
"matchMode": "CLASSIC",
"matchType": "MATCHED_GAME",
"matchCreation": 1427867835805,
"matchDuration": 3424,
"queueType": "RANKED_SOLO_5x5",
"mapId": 11,
"season": "SEASON2015",
"matchVersion": "5.6.0.194",
"participants": [
// more dictionaries
],
"participantIdentities": [
// more dictionaries
],
"teams": [
// more dictionaries
],
"timeline": {
"frames": [
// many frame dictionaries
],
"frameInterval": 60000
}
},
// more dictionaries
]
}
I saved it as matches1.json in the same directory as my code.
I have also tried putting
match_histories={}
before my other code, but that didn't help either.
How can I save this .json file as a dictionary containing dictionaries?
match_histories is a dictionary with one key, matches. The value is a list of dictionaries; loop over that list:
for match in match_histories['matches']:
print(match['matchId'])
Warning: the match objects are themselves large dictionaries.
Related
I would like to browse this dictionary more particularly the key lines
dict= {
"comm": [
{
"palo": "strcc",
"masd": "tete",
"lignes": [
{
"salti": "namora",
"fasha": "ben"
}
]
}
]
}
And to display "salti" and "fasha" I have an error
print(dict['comm']['lignes']['salti'])
"comm" and "lines" in your dictionary are lists. So you need to add the index item you want to access:
print(dict['comm'][0]['lignes'][0]['salti'])
I have a json file with this structure:
[
{
"_id": "62b2ebff955fe1001d225781",
"datasetName": "comments",
"action": "dataset",
"comment": "Initial data!",
"instances": [
"62b2eb94955fe1001d22576a",
"62b2eba1955fe1001d22576e",
"62b2eba9955fe1001d225770",
"62b2ebb9955fe1001d225772",
"62b2ebcc955fe1001d225774",
"62b2ebe2955fe1001d225778"
],
"label": [
"Contemporary",
"Tango"
]
}
]
I wanted to know how can I access the values of "label" and also how can I count the length of "instances" object.
So your json is a list of dict. You can load it with the json standard library. Then you can iterate over the elements in the list. As each of these elements is a dict item, you can then get the instances list by it's key. As it is a list, you can simple call the len() function to get the length.
See the code below as an example:
import json
with open("somefile.json") as infile:
data = json.load(infile)
for element in data:
print(len(element["instances"]))
Note that if your json file contains more elements in the root list, it will of course print out the length of the instances list for each of these elements.
I have 4 lists of floats which I have extracted from a JSON file. I have to modify the values in these 4 lists simultaneously, depending on the first list. I am using zip() to iterate over all the 4 lists, but I am unable to update the values in the original dictionary representing the JSON.
I need to check the sign of an element in first list, if it is negative, all the values in the 4 lists which have the same index as that element have to be zeroed out. The following code snippet contains some sample data:
{
"channels": [
{
"name": "TTZAR1e",
"samples": [
{
"data": [0.0996781, 0.0177724, -0.00566106],
"modifiers": [
{"data": [0.084338, 0.0103356, 0.010294], "type": "staterror"},
{"data": {"hi_data": [0.0996781, 0.0177724, -0.00566106], "lo_data": [0.0996781, 0.0177724, -0.00566106]}, "type": "histosys"}
],
"name": "conv"
}
]
}
]
}
And here's what I've tried so far:
import json
file = open("test1.json", 'r')
json_data = json.load(file)
for key, value in json_data.items():
for i in value:
for samp in i.get('samples'):
for mod in samp.get('modifiers'):
hi_list=[]
lo_list=[]
if(mod.get('type') == 'staterror'):
stat_list = mod.get('data')
if(mod.get('type') == 'histosys'):
hi_list = mod.get('data').get('hi_data')
lo_list = mod.get('data').get('lo_data')
for val, val2, val3, val4 in zip(samp.get('data'), hi_list, lo_list, stat_list):
if (val<0):
val,val2,val3,val4 = 0,0,0,0
When I print the JSON, I still get the original negative values. The final output I am working towards is something like the following:
{
"channels": [
{
"name": "TTZAR1e",
"samples": [
{
"data": [0.0996781, 0.0177724, 0],
"modifiers": [
{"data": [0.084338, 0.0103356, 0],"type": "staterror"},
{"data": {"hi_data": [0.0996781, 0.0177724, 0], "lo_data": [0.0996781, 0.0177724, 0]}, "type": "histosys"}
],
"name": "conv"
}
]
}
]
}
I would like to know how to update the values in the dictionary itself. Is there a way to implement this here?
zip() creates a list of tuples. These tuples do not share any memory with the original lists, meaning that you have no way of mutating the inputs to zip() using what it returns.
You should store references to the lists that you want to modify, and then modify them in-place. (I will note that you should seriously consider simplifying this code, as the high nesting depth makes this code hard to read and debug.) Here's a code snippet that does that:
for key, value in json_data.items():
for i in value:
for samp in i.get('samples'):
lists_to_modify = [samp.get('data')]
for mod in samp.get('modifiers'):
if(mod.get('type') == 'staterror'):
lists_to_modify.append(mod.get('data'))
if(mod.get('type') == 'histosys'):
lists_to_modify.append(mod.get('data').get('hi_data'))
lists_to_modify.append(mod.get('data').get('lo_data'))
for data_index in range(len(samp.get('data'))):
if samp.get('data')[data_index] < 0:
for list_to_modify in lists_to_modify:
list_to_modify[data_index] = 0
I know this is a worst title but let me explain the question by sample. My data is:
data = [
{
"subdata": [ # subdata various number of dictionaries of same keys, including "ext_id", ...
{
"ext_id": "12345", # ... but of different values
...
},
{
"ext_id": "54321",
...
}
],
...
},
... # and many other dictionary items with "subdata", which in turn contains
# a list of dictionaries containing "ext_id" and corresponding values
]
my goal is make a list of the pair of "ext_id"s in "subdata", i.e.
goal = [
("12345", "54321"),
(...)
]
I know a for-loop is okay for this goal, but wondering if a list comprehension is possible? I tried this:
goal = [x["ext_id"] for y in data for x in y["subdata"]]
and get a flattened version of goal ["12345", "54321", ...] rather than a 2-D list.
Any advices are appreciated.
If you have a data structure like this:
data = [
{
"subdata": [
{
"ext_id": "12345",
},
{
"ext_id": "54321",
}
],
},
{
"subdata": [
{
"ext_id": "98765",
},
{
"ext_id": "56789",
}
],
}
]
Then to get the output that you want, you could use list comprehension (and a generator comprehension too) as follows:
goal = [tuple(dict_['ext_id'] for dict_ in subdata['subdata']) for subdata in data ]
goal will contain:
[('12345', '54321'), ('98765', '56789')]
Yes you can use list comprehension here. Following code will give you your desired results
[tuple(sub_dict.get('ext_id') for sub_dict in dictionary.get('subdata')) for dictionary in data]
I want to retrieve all the IP address range from Azure cloud from here
The data after conversion in json is in the following format:
{
"AzurePublicIpAddresses": {
"Region": [
{
...
"IpRange": [
{
"_Subnet": "40.69.96.0/19"
},
{
"_Subnet": "40.86.192.0/18"
}
],
"_Name": "canadaeast"
},
{
"IpRange": [
{
"_Subnet": "13.71.160.0/19"
},
{
"_Subnet": "13.88.224.0/19"
},
{
"_Subnet": "40.85.192.0/18"
}
],
"_Name": "canadacentral"
}
],
"_xmlns:xsd": "http://www.w3.org/2001/XMLSchema",
"_xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance"
}
}
I am unable to access IP ranges? through this code?
with open('AZURE_IP.json') as data_file:
data = json.load(data_file)
list_IPCIDR = []
for i in data["AzurePublicIpAddresses"]:
for ii in i["Region"]:
for j in ii["IpRange"]:
list_IPCIDR.append(i["_Subnet"])
sys.stdout=open("test2.txt","w")
data["AzurePublicIpAddresses"] is a dict. Iterating directly over a dict just gives you the the keys of that dict.
So
for i in data["AzurePublicIpAddresses"]:
print(i)
will print
Region
_xmlns:xsd
_xmlns:xsi
in some order.
You can get the Subnet IP ranges like this:
list_IPCIDR = []
for ipr in data["AzurePublicIpAddresses"]["Region"]:
for d in ipr["IpRange"]:
list_IPCIDR.append(d["_Subnet"])
print(list_IPCIDR)
output
['40.69.96.0/19', '40.86.192.0/18', '13.71.160.0/19', '13.88.224.0/19', '40.85.192.0/18']
This works because data["AzurePublicIpAddresses"]["Region"] is a list of dicts. Each of those dict (that are temporarily bound to the name ipr) contains a list of dicts associated with the "IpRange" key, so we need to iterate over those lists in the inner loop, and then extract the subnet strings from those inner dicts.
If you like you can do this in a list comprehension, butI advise splitting it up over several lines, eg:
list_IPCIDR = [d["_Subnet"]
for ipr in data["AzurePublicIpAddresses"]["Region"]
for d in ipr["IpRange"]]
It's often desirable to iterate over the (key, value) pairs of a dict. You can do that using the .items method (or .iteritems in Python 2). Eg,
list_IPCIDR = []
for key, val in data["AzurePublicIpAddresses"].items():
if key == "Region":
for dct in val:
for s in dct["IpRange"]:
list_IPCIDR.append(s["_Subnet"])
AzurePublicIpAddresses is a dictionary, so:
for i in data["AzurePublicIpAddresses"]:
Iterates through the keys (which are strings, in this case). I.e. you're trying to do "Region"["Region"], which is string slicing. Try something more like:
for i in data["AzurePublicIpAddresses"]:
for ii in data["Azure..."][i]:
# Use ii as it is the contents of the 'Region' attribute
if type(ii) == list: # Sometimes ii is a string, like at the end of your data.
list_IPCIDR.append(ii["IpRange"]["_Subnet"])