Ailton Ailton - 3 months ago 11
Python Question

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'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}]


I tried this way, but still remain some dicts inside sub-lists

import urllib2
import json

response = urllib2.urlopen('http://www.site.com.br/produto/sku/5293')
data = json.load(response)

for a in data:
for key, value in a.items():
print key, value


This is the result, but as you guys can see, still remain some key-value inside the sub-list

SkuSellersInformation : [{u'Name': u'site', u'Price': 409, u'IsDefaultSeller': True, u'AvailableQuantity': 2, u'LogoUrl': None, u'SellerId': u'1', u'ListPrice': 409}]
BestInstallmentNumber : 10
RealWeightKg : 100.0
NotifyMe : True
HasServiceAtCartPage : False
RewardValue : 0.0
ListPrice : 409
Name : Light Blue Eau de Toilette Dolce & Gabbana - Perfume Feminino - 50ml
HasExtendedWarranty : False
BestInstallmentValue : 40.9
Ean : 0737052074313
Price : 409
RealWidth : 10.0
IdProduct : 909
AvailabilityMessage : True
HasServiceAtServicePage : False
RealLength : 10.0
RealHeight : 10.0
HasServiceAtProductPage : False
SalesChannel : 1
DefaultSellerId : 1
Reference : 002796
HasExtendedWarrantyPage : False
Id : 5293
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}]]
Availability : True

Answer

Since JSON allow 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.

import json

def get_all(myjson):
    """ Recursively find the keys and associated values in all the dictionaries in
        in the json object or list.
    """
    if isinstance(myjson, dict):
        for jsonkey, jsonvalue in myjson.items():
            if not isinstance(jsonvalue, (dict, list)):
                yield jsonkey, jsonvalue
            else:
                for k, v in get_all(jsonvalue):
                    yield k, v
    elif isinstance(myjson, list):
        for element in myjson:
            if isinstance(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))