i got a problem to convert, try to google this subject without success in elegant way
I have 2 dictionnaries: dictobj and dictgrp.
Dictobj containt simple key value
Dictgrp containt group list. Each entry containt list objects and/or list group.
I need to decode all entries from group.
"obj1": "value1",
"obj2": "value2",
"obj3": "value3",
"obj4": "value3",
...
}
dictgrp:{
"maingrp":{
"obj": ["obj1", "obj2", "obj3", "obj4"],
"grp": ["grp1", "grp2", "grp3"]
},
...
"grp1": {
"obj": ["obj11", "obj12"}
},
"grp2": {
"obj": ["srv21"],
"grp": ["grp21", "grp22"]
},
"grp3": {
"grp:["grp31"]
},
"grp21": {
"obj":["srv211", "srv212"]
},
"grp22": {
"obj":["srv221", "srv222"]
},
"grp31":{
"grp":["grp311"]
},
"grp311":{
"obj":["srv3111"]
},
...
}
So i need to translate all values from each group. What is the best/elegant way to translate this:
maingrp
obj1 value1
obj2 value2
obj3 value3
grp1 obj11 value11
grp1 obj12 value12
grp2 srv21 value21
grp2 grp21 srv211 value211
grp2 grp21 srv212 value212
grp2 grp22 srv221 value221
grp2 grp22 srv222 value222
grp3 grp31 grp311 srv3111 value3111
Thank oyu
Successed in first level (object), but dont know how to resolv nested group
Related
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 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']]
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']}
I have this structure, converted using json.load(json)
jsonData = [ {
thing: [
name: 'a name',
keys: [
key1: 23123,
key2: 83422
]
thing: [
name: 'another name',
keys: [
key1: 67564,
key2: 93453
]
etc....
} ]
I have key1check = 67564,
I want to check if a thing's key1 matches this value
if key1check in val['thing']['keys']['key1'] for val in jsonData:
print ('key found, has name of: {}'.format(jsonData['thing']['name'])
Should this work? Is there a better was to do this?
Not quite:
in is for inclusion in a sequence, such as a string or a list. You're comparing integer values, so a simple == is what you need.
Your given structure isn't legal Python: you have brackets in several places where you're intending a dictionary; you need braces instead.
Otherwise, you're doing fine ... but you should not ask us if it will work: ask the Python interpreter by running the code.
Try this for your structure:
jsonData = [
{ "thing": {
"name": 'a name',
"keys": {
"key1": 23123,
"key2": 83422
} } },
{ "thing": {
"name": 'another name',
"keys": {
"key1": 67564,
"key2": 93453
} } }
]
You can loop through #Prune 's dictionary using something like this as long as the structure is consistent.
for item in jsonData:
if item['thing']['keys']['key1'] == key1check:
print("true")
else:
print("false")
I basically have a file with this structure:
root \
{
field1 {
subfield_a {
"value1"
}
subfield_b {
"value2"
}
subfield_c {
"value1"
"value2"
"value3"
}
subfield_d {
}
}
field2 {
subfield_a {
"value1"
}
subfield_b {
"value1"
}
subfield_c {
"value1"
"value2"
"value3"
"value4"
"value5"
}
subfield_d {
}
}
}
I want to parse this file with python to get a multidimensional array that contains all the values of a specific subfield (for examples subfield_c). E.g. :
tmp = magic_parse_function("subfield_c",file)
print tmp[0] # [ "value1", "value2", "value3"]
print tmp[1] # [ "value1", "value2", "value3", "value4", "value5"]
I'm pretty sure I've to use the pyparsing class, but I don't where to start to set the regex (?) expression. Can someone give me some pointers ?
You can let pyparsing take care of the matching and iterating over the input, just define what you want it to match, and pass it the body of the file as a string:
def magic_parse_function(fld_name, source):
from pyparsing import Keyword, nestedExpr
# define parser
parser = Keyword(fld_name).suppress() + nestedExpr('{','}')("content")
# search input string for matching keyword and following braced content
matches = parser.searchString(source)
# remove quotation marks
return [[qs.strip('"') for qs in r[0].asList()] for r in matches]
# read content of file into a string 'file_body' and pass it to the function
tmp = magic_parse_function("subfield_c",file_body)
print(tmp[0])
print(tmp[1])
prints:
['value1', 'value2', 'value3']
['value1', 'value2', 'value3', 'value4', 'value5']