How To Extract Only The Key-value From This List-dict Json Response?
I would like how can I extract only the keys and values from this json response: [{u'SkuSellersInformation': [{u'Name': u'site', u'Price': 409, u'IsDefaultSeller': True, u'Availabl
Solution 1:
Since JSON allows the definition of recursive data structures, the following recursive function will find all the key, value pairs in all the dictionaries encountered in one. Note it may yield the same key multiple times when it occurs in more than one (nested or parallel) dictionary.
defget_all(myjson):
""" Recursively find the keys and associated values in all the dictionaries
in the json object or list.
"""ifisinstance(myjson, dict):
for jsonkey, jsonvalue in myjson.items():
ifnotisinstance(jsonvalue, (dict, list)):
yield jsonkey, jsonvalue
else:
for k, v in get_all(jsonvalue):
yield k, v
elifisinstance(myjson, list):
for element in myjson:
ifisinstance(element, (dict, list)):
for k, v in get_all(element):
yield k, v
data = [{u'SkuSellersInformation': [{u'Name': u'site', u'Price': 409, u'IsDefaultSeller': True, u'AvailableQuantity': 2, u'LogoUrl': None, u'SellerId': u'1', u'ListPrice': 409}], u'BestInstallmentNumber': 10, u'RealWeightKg': 100.0, u'NotifyMe': True, u'HasServiceAtCartPage': False, u'RewardValue': 0.0, u'ListPrice': 409, u'Name': u'Light Blue Eau de Toilette Dolce & Gabbana - Perfume Feminino - 50ml', u'HasExtendedWarranty': False, u'BestInstallmentValue': 40.9, u'Ean': u'0737052074313', u'Price': 409, u'RealWidth': 10.0, u'IdProduct': 909, u'AvailabilityMessage': u'True', u'HasServiceAtServicePage': False, u'RealLength': 10.0, u'RealHeight': 10.0, u'HasServiceAtProductPage': False, u'SalesChannel': u'1', u'DefaultSellerId': u'1', u'Reference': u'002796', u'HasExtendedWarrantyPage': False, u'Id': 5293, u'Images': [[{u'Path': u'http://site.vteximg.com.br/arquivos/ids/183280-320-320/light-blue-edt-dg-2.jpg', u'ArchiveTypeId': 2, u'Name': None, u'IdArchive': u'183280', u'IsMain': False}, {u'Path': u'http://site.vteximg.com.br/arquivos/ids/183280-55-55/light-blue-edt-dg-2.jpg', u'ArchiveTypeId': 3, u'Name': None, u'IdArchive': u'183280', u'IsMain': False}, {u'Path': u'http://site.vteximg.com.br/arquivos/ids/183280-65-65/light-blue-edt-dg-2.jpg', u'ArchiveTypeId': 1, u'Name': None, u'IdArchive': u'183280', u'IsMain': False}, {u'Path': u'http://site.vteximg.com.br/arquivos/ids/183280-500-500/light-blue-edt-dg-2.jpg', u'ArchiveTypeId': 10, u'Name': None, u'IdArchive': u'183280', u'IsMain': False}, {u'Path': u'http://site.vteximg.com.br/arquivos/ids/183280-120-120/light-blue-edt-dg-2.jpg', u'ArchiveTypeId': 29, u'Name': None, u'IdArchive': u'183280', u'IsMain': False}, {u'Path': u'http://site.vteximg.com.br/arquivos/ids/183280-130-130/light-blue-edt-dg-2.jpg', u'ArchiveTypeId': 30, u'Name': None, u'IdArchive': u'183280', u'IsMain': False}], [{u'Path': u'http://site.vteximg.com.br/arquivos/ids/187857-320-320/light-blue-eau-de-toilette-dolce-gabbana-perfume-feminino.jpg', u'ArchiveTypeId': 2, u'Name': None, u'IdArchive': u'187857', u'IsMain': True}, {u'Path': u'http://site.vteximg.com.br/arquivos/ids/187857-55-55/light-blue-eau-de-toilette-dolce-gabbana-perfume-feminino.jpg', u'ArchiveTypeId': 3, u'Name': None, u'IdArchive': u'187857', u'IsMain': True}, {u'Path': u'http://site.vteximg.com.br/arquivos/ids/187857-65-65/light-blue-eau-de-toilette-dolce-gabbana-perfume-feminino.jpg', u'ArchiveTypeId': 1, u'Name': None, u'IdArchive': u'187857', u'IsMain': True}, {u'Path': u'http://site.vteximg.com.br/arquivos/ids/187857-500-500/light-blue-eau-de-toilette-dolce-gabbana-perfume-feminino.jpg', u'ArchiveTypeId': 10, u'Name': None, u'IdArchive': u'187857', u'IsMain': True}, {u'Path': u'http://site.vteximg.com.br/arquivos/ids/187857-120-120/light-blue-eau-de-toilette-dolce-gabbana-perfume-feminino.jpg', u'ArchiveTypeId': 29, u'Name': None, u'IdArchive': u'187857', u'IsMain': True}, {u'Path': u'http://site.vteximg.com.br/arquivos/ids/187857-130-130/light-blue-eau-de-toilette-dolce-gabbana-perfume-feminino.jpg', u'ArchiveTypeId': 30, u'Name': None, u'IdArchive': u'187857', u'IsMain': True}]], u'Availability': True}]
for key, value in get_all(data):
print('{!r}: {!r}'.format(key, value))
Post a Comment for "How To Extract Only The Key-value From This List-dict Json Response?"