Re-edited to make more clear and simple
For below data
[
{
"name": "name1",
"a_id": "12345",
"b_id": "0d687c94c5f4"
},
{
"name": "name2",
"a_id": "67890",
"b_id": "0d687c94c5f4"
},
{
"name": "name3",
"a_id": "23857",
"b_id": "9ec34be3d535"
},
{
"name": "name4",
"a_id": "84596",
"b_id": "9ec34be3d535"
},
{
"name": "name5",
"a_id": "d82ebe9815cc",
"b_id": null
}
]
How to get
based on "b_id" "0d687c94c5f4":
id1 = 12345
id2 = 67890
based on "b_id" "9ec34be3d535":
id3 = 23857
id4 = 84596
result = collections.defaultdict(list)
for res in response:
result[res['b_id']].append(res['a_id'])
result:
defaultdict(list,
{'0d687c94c5f4': ['12345', '67890'],
'9ec34be3d535': ['23857', '84596'],
None: ['d82ebe9815cc']})
result = {
item['b_id']: [
subitem['a_id']
for subitem in response
if subitem['b_id'] == item['b_id']
]
for item in response
}
print(result)
>>> {'9ec34be3d535': ['23857', '84596'], '0d687c94c5f4': ['12345', '67890'], None: ['d82ebe9815cc']}
Your request is not very clear.. but I think you mean you want to regroup the list of json with a different key... you can use itertools for that
try this:
import itertools
for key, group in itertools.groupby(r, lambda item: item['b_id']):
print 'b_id', key, [x['a_id'] for x in group]
b_id 0d687c94c5f4 ['12345', '67890']
b_id 9ec34be3d535 ['23857', '84596']
b_id None ['d82ebe9815cc']
or in dictionary form
for key, group in itertools.groupby(r, lambda item: item['b_id']):
print {key: [x['a_id'] for x in group]}
{'0d687c94c5f4': ['12345', '67890']}
{'9ec34be3d535': ['23857', '84596']}
{None: ['d82ebe9815cc']}
Related
I have an object on dynamo db saved in the following way:
{
"name": "nameA",
...,
"properties": {
"prop1": "a",
...
}
}
If I pass the following object:
{
"name": "nameUpdate"
}
I would like to eventually get the following result:
{
"name": "nameUpdate",
...,
"properties": {
"prop1": "a",
...
}
}
The problem is that I get the object without the nested field:
{
"name": "nameUpdate",
...,
"properties": {}
}
MY APPROACH
To perform the update operation I am proceeding as follows:
def build_ddb_update_expression(data):
prefix = '#pf'
vals = {}
exp = 'SET '
attr_names = {}
for key, value in data.items():
vals[f':{key}'] = value
attr_names[f'#pf_{key}'] = key
exp += f'{prefix}_{key} = :{key}, '
exp = exp.rstrip(", ")
return vals, exp, attr_names
...
vals, exp, attr_names = build_ddb_update_expression(
json.loads(json.dumps(object_to_update), parse_float=decimal.Decimal))
response = table.update_item(
Key={'object_id': object_id},
ConditionExpression='attribute_exists(object_id)',
UpdateExpression=exp,
ExpressionAttributeValues=vals,
ExpressionAttributeNames=attr_names,
ReturnValues="ALL_NEW"
)
Has this ever happened to anyone?
Thanks in advance
I have some json that I would like to transform from this:
[
{
"name":"field1",
"intValue":"1"
},
{
"name":"field2",
"intValue":"2"
},
...
{
"name":"fieldN",
"intValue":"N"
}
]
into this:
{ "field1" : "1",
"field2" : "2",
...
"fieldN" : "N",
}
For each pair, I need to change the value of the name field to a key, and the values of the intValue field to a value. This doesn't seem like flattening or denormalizing. Are there any tools that might do this out-of-the-box, or will this have to be brute-forced? What's the most pythonic way to accomplish this?
parameters = [ # assuming this is loaded already
{
"name":"field1",
"intValue":"1"
},
{
"name":"field2",
"intValue":"2"
},
{
"name":"fieldN",
"intValue":"N"
}
]
field_int_map = dict()
for p in parameters:
field_int_map[p['name']] = p['intValue']
yields {'field1': '1', 'field2': '2', 'fieldN': 'N'}
or as a dict comprehension
field_int_map = {p['name']:p['intValue'] for p in parameters}
This works to combine the name attribute with the intValue as key:value pairs, but the result is a dictionary instead of the original input type which was a list.
Use dictionary comprehension:
json_dct = {"parameters":
[
{
"name":"field1",
"intValue":"1"
},
{
"name":"field2",
"intValue":"2"
},
{
"name":"fieldN",
"intValue":"N"
}
]}
dct = {d["name"]: d["intValue"] for d in json_dct["parameters"]}
print(dct)
# {'field1': '1', 'field2': '2', 'fieldN': 'N'}
I have the following data:
data={
"locations": [
{
"id": "27871f2d-101c-449e-87ad-36a663b144fe",
"switch_id": 20,
"switch_port": 16,
"vlan_id": 101,
},
{
"id": "94b1d7a2-7ff2-4ba3-8259-5eb7ddd09fe1",
"switch_id": 6,
"switch_port": 24,
"vlan_id": 203,
},
]
}
And what I want to do is extract 'id' and 'vlan_id' into a new dictionary with a list of sub dictionaries, like this:
new_data={
"connections": [
{
"id": "27871f2d-101c-449e-87ad-36a663b144fe",
"vlan_id": 101,
},
{
"id": "94b1d7a2-7ff2-4ba3-8259-5eb7ddd09fe1",
"vlan_id": 203,
},
]
}
My initial thoughts were as a dictionary comprehension like this:
new_data = {"connections": [some code here]}
But not sure of the some code bit yet.
Try:
new_data = {"connections": [{'id': d['id'], 'vlan_id': d['vlan_id']} for d in data['locations']]}
{'connections': [{'id': '27871f2d-101c-449e-87ad-36a663b144fe', 'vlan_id': 101}, {'id': '94b1d7a2-7ff2-4ba3-8259-5eb7ddd09fe1', 'vlan_id': 203}]}
You can create the new_data variable accesing the first dictionary data like this:
new_data={
"connections": [
{
"id": data['locations'][0]['id'],
"vlan_id": data['locations'][0]['vlan_id'],
},
{
"id": data['locations'][1]['id'],
"vlan_id": data['locations'][1]['vlan_id'],
},
]
}
edit:
You can get a more dynamic approach by reading every object in the list with a forloop like this:
new_data={
"connections": []
}
for object in data['locations']:
new_dict = {
"id": object["id"],
"vlan_id": object["vlan_id"]
}
new_data['connections'].append(new_dict)
Following Marc's answer here, you could modify it to
new_data = {}
for i in range(len(data['locations'])):
if "connections" not in new_data.keys():
new_data['connections'] = [{"id": data['locations'][i]['id'],"vlan_id": data['locations'][i]['vlan_id']}]
else:
new_data['connections'].append({"id": data['locations'][i]['id'],"vlan_id": data['locations'][i]['vlan_id']})
The Answers here are good but you can make the code more dynamic
keys_to_extract = ['id', 'vlan_id']
locations = data['locations']
connections = { key: val for key, val in locations.items() if key in keys_to_extract }
new_data = {'connections': connections}
Now you can change the keys you need on the fly
I have the below python dictionary stored as dictPython
{
"paging": {"count": 10, "start": 0, "links": []},
"elements": [
{
"organizationalTarget~": {
"vanityName": "vv",
"localizedName": "ViV",
"name": {
"localized": {"en_US": "ViV"},
"preferredLocale": {"country": "US", "language": "en"},
},
"primaryOrganizationType": "NONE",
"locations": [],
"id": 109,
},
"role": "ADMINISTRATOR",
},
],
}
I need to get the values of vanityName, localizedName and also the values from name->localized and name->preferredLocale.
I tried dictPython.keys() and it returned dict_keys(['paging', 'elements']).
Also I tried dictPython.values() and it returned me what is inside of the parenthesis({}).
I need to get [vv, ViV, ViV, US, en]
I am writing this in a form of answer, so I can get to explain it better without the comments characters limit
a dict in python is an efficient key/value structure or data type
for example dict_ = {'key1': 'val1', 'key2': 'val2'} to fetch key1 we can do it in 2 different ways
dict_.get(key1) this returns the value of the key in this case val1, this method has its advantage, that if the key1 is wrong or not found it returns None so no exceptions are raised. You can do dict_.get(key1, 'returning this string if the key is not found')
dict_['key1'] doing the same .get(...) but will raise a KeyError if the key is not found
So to answer your question after this introduction,
a dict can be thought of as nested dictionaries and/or objects inside of one another
to get your values you can do the following
# Fetch base dictionary to make code more readable
base_dict = dict_["elements"][0]["organizationalTarget~"]
# fetch name_dict following the same approach as above code
name_dict = base_dict["name"]
localized_dict = name_dict["localized"]
preferred_locale_dict = name_dict ["preferredLocale"]
so now we fetch all of the wanted data in their corresponding locations from your given dictionary, now to print the results, we can do the following
results_arr = []
for key1, key2 in zip(localized_dict, preferredLocale_dict):
results_arr.append(localized_dict.get(key1))
results_arr.append(preferred_locale_dict.get(key2))
print(results_arr)
What about:
dic = {
"paging": {"count": 10, "start": 0, "links": []},
"elements": [
{
"organizationalTarget~": {
"vanityName": "vv",
"localizedName": "ViV",
"name": {
"localized": {"en_US": "ViV"},
"preferredLocale": {"country": "US", "language": "en"},
},
"primaryOrganizationType": "NONE",
"locations": [],
"id": 109,
},
"role": "ADMINISTRATOR",
},
],
}
base = dic["elements"][0]["organizationalTarget~"]
c = base["name"]["localized"]
d = base["name"]["preferredLocale"]
output = [base["vanityName"], base["localizedName"]]
output.extend([c[key] for key in c])
output.extend([d[key] for key in d])
print(output)
outputs:
['vv', 'ViV', 'ViV', 'US', 'en']
So something like this?
[[x['organizationalTarget~']['vanityName'],
x['organizationalTarget~']['localizedName'],
x['organizationalTarget~']['name']['localized']['en_US'],
x['organizationalTarget~']['name']['preferredLocale']['country'],
x['organizationalTarget~']['name']['preferredLocale']['language'],
] for x in s['elements']]
"fwt-master2": {
"ipv4": {
"rtr": {
"ip": "1.2.3.4",
"net": "3.4.5.6",
"netlen": "24",
"netmask": "255.255.255.0",
"broadcast": "7.8.9.1"
}
}
I am trying to get the ip value from this JSON file without specifying the values of each element (without using fwt-maser2[ipv4][rtr][ip]).
using the .values() method (.values()[0].values()[0].values()[0]`)
I am getting the netlen value (24) instead of the ip values which is actually the first element.
why is such thing happening?
I think use of nested code to find key value is the best way ...this way you just search if "broadcast" key in dict then print its value
Try something rom here: find all occurances of a key
If you only know that the target key is "ip", then you can use recursion:
s = {"fwt-master2": {
"ipv4": {
"rtr": {
"ip": "1.2.3.4",
"net": "3.4.5.6",
"netlen": "24",
"netmask": "255.255.255.0",
"broadcast": "7.8.9.1"
}
}
}
}
def get_ip(d):
return [i for c in filter(None, [b if a == 'ip' else get_ip(b) if isinstance(b, dict) else None for a, b in d.items()]) for i in c]
print(''.join(get_ip(s)))
Output:
1.2.3.4
I decided to go through your dictionary and found that its incomplete !!!
your dictionary :
"fwt-master2": {
"ipv4": {
"rtr": {
"ip": "1.2.3.4",
"net": "3.4.5.6",
"netlen": "24",
"netmask": "255.255.255.0",
"broadcast": "7.8.9.1"
}
}
Actually it should be like : Added missing curly braces ... first and last two..
{"fwt-master2": { "ipv4": { "rtr": { "ip": "1.2.3.4", "net": "3.4.5.6", "netlen": "24", "netmask": "255.255.255.0", "broadcast": "7.8.9.1" }}}}
Well it happens ... so Amusing above updated one is the actual dictionary so here is how you can achieve your goal:
>>> d = {"fwt-master2": { "ipv4": { "rtr": { "ip": "1.2.3.4", "net": "3.4.5.6", "netlen": "24", "netmask": "255.255.255.0", "broadcast": "7.8.9.1" }}}}
>>> obj = []
>>> obj.append(d)
>>> obj
[{'fwt-master2': {'ipv4': {'rtr': {'net': '3.4.5.6', 'netlen': '24', 'ip': '1.2.3.4', 'netmask': '255.255.255.0', 'broadcast': '7.8.9.1'}}}}]
>>> key_list = ['netmask', 'broadcast', 'ip', 'net']
>>> def recursive_items(dictionary):
... for key, value in dictionary.items():
... if type(value) is dict:
... yield from recursive_items(value)
... else:
... yield (key, value)
...
>>> def find_key(obj):
... for e in obj:
... for key, value in recursive_items(e):
... if key in key_list:
... print(key, value)
... for key, value in recursive_items(e):
... if key in key_list and value == 0:
... print(double_quote(key),":", value)
...
>>> find_key(obj)
net 3.4.5.6
ip 1.2.3.4
netmask 255.255.255.0
broadcast 7.8.9.1
Have Fun