Skip to content

Instantly share code, notes, and snippets.

@elcolumbio
Created September 1, 2017 19:51
Show Gist options
  • Save elcolumbio/dfbbc26e2585425e2f4d47879bc9c584 to your computer and use it in GitHub Desktop.
Save elcolumbio/dfbbc26e2585425e2f4d47879bc9c584 to your computer and use it in GitHub Desktop.
mws api python, flatten response dict
# maybe its helpful or you see improvements
# i get lots of latin-1 encoded text fields, so i use this try block
def flatten_dict(d):
def items():
for key, value in d.items():
if isinstance(value, dict):
for subkey, subvalue in flatten_dict(value).items():
try:
subvalue = subvalue.encode('latin-1').decode('utf-8')
except:
pass
yield key + "." + subkey, subvalue
elif isinstance(value, list):
for deep in value:
for xkey, xvalue in deep.items():
if isinstance(xvalue, dict):
for xsubkey, xsubvalue in flatten_dict(xvalue).items():
try:
xsubvalue = xsubvalue.encode('latin-1').decode('utf-8')
except:
pass
yield xkey + "." + xsubkey, xsubvalue
else:
try:
value.encode('latin-1').decode('utf-8')
except:
pass
yield key, value.encode('latin-1').decode('utf-8')
return dict(items())
#i call it like that
for item in resultlist:
todflist.append(flatten_dict(item))
@elcolumbio
Copy link
Author

my yaml file looks like that (some reports i have to test):
refundeventlist :
toplevel_keys : [amazonorderid, marketplacename, posteddate, quantityshipped, sellerorderid, sellersku]
secondlevel_lists :
shipmentitemadjustmentlist:
itemchargeadjustmentlist : [chargetype, currencyamount, currencycode]
itemfeeadjustmentlist : [feetype, currencyamount, currencycode]

shipmenteventlist :
toplevel_keys : [amazonorderid, marketplacename, posteddate, quantityshipped, sellersku]
secondlevel_lists :
shipmentitemlist :
itemchargelist : [chargetype, currencyamount, currencycode]
itemfeelist : [feetype, currencyamount, currencycode]

chargebackeventlist :
toplevel_keys : [amazonorderid, marketplacename, posteddate, quantityshipped, sellersku]
secondlevel_lists :
shipmentitemadjustmentlist:
itemchargeadjustmentlist : [chargetype, currencyamount, currencycode]
itemfeeadjustmentlist : [feetype, currencyamount, currencycode]

servicefeeeventlist :
toplevel_keys : []
secondlevel_lists :
dummy:
feelist : [feetype, currencyamount, currencycode]

adjustmenteventlist :
toplevel_keys : [adjustmenttype, posteddate]
toplevel_lists :
adjustmentamount : [currencyamount, currencycode]
adjustmentitemlist : [quantity, sellersku]

productadspaymenteventlist :
toplevel_keys : [invoiceid, posteddate, transactiontype]
toplevel_lists :
taxvalue : [appendkey, currencycode, currencyamount]
transactionvalue : [appendkey, currencycode, currencyamount]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment