Lucas Mengual
Lucas Mengual

Reputation: 415

Flatten nested dictionary to key and joined string value

I need help with a function to flatten a nested dictionary in the following format:

dict_test = {
    "id" : "5d4c2c0fd89234260ec81",
    "Reference Number" : "JA-L800D-191",
    "entities_discovered" : {
        "OTHER_ID" : [ 
            "L800DFAG02191"
        ],
        "CODE_ID" : [ 
            "160472708",
            "276954773"
        ]
    },
    "label_field" : [ 
        "ELECTRONICS",
        "HDMI"
    ],
    "numeric_field" : [ 
        491, 
        492
    ],

}

The function I was working with, flattens the dictionary to one dimension (key:value) as I want, but doesn´t join the values within the same key iteration.

def flatten(d):
    agg = {}
    def _flatten(d, prev_key=''):
        if isinstance(d, list):
            for i, item in enumerate(d):
                new_k = '%s.%s' % (prev_key, i) if prev_key else i
                _flatten(item, prev_key=new_k)
        elif isinstance(d, dict):
            for k, v in d.items():
                new_k = '%s.%s' % (prev_key, k) if prev_key else k
                _flatten(v, prev_key=new_k)
        else:
            agg[prev_key] = d

    _flatten(d)
    return agg

My current output is:

{
    "id" : "5d4c2c0fd89234260ec81",
    "Reference Number" : "JA-L800D-191",
    "entities_discovered.OTHER_ID.0" : "L800DFAG02191",
    "entities_discovered.CODE_ID.0" : "160472708",
    "entities_discovered.CODE_ID.1" : "276954773",
    "label_field.0" : "ELECTRONICS",
    "label_field.1" : "HDMI",
    "numeric_field.0" : 491, 
    "numeric_field.1" : 492
}

But actually I´m looking for something like (joining the values into the same string and separated by , or |):

{
    "id" : "5d4c2c0fd89234260ec81",
    "Reference Number" : "JA-L800D-191",
    "OTHER_ID" : "L800DFAG02191",
    "CODE_ID" : "160472708, 276954773",
    "label_field" : "ELECTRONICS, HDMI",
    "numeric_field" : ¨491, 492¨
}

Upvotes: 0

Views: 277

Answers (4)

smerllo
smerllo

Reputation: 3375

def flatten(dict_test): 
    for key in ['label_field', 'numeric_field']: 
        dict_test[key]= ', '.join([str(c) for c in dict_test[key]])

    for c in dict_test['entities_discovered'].keys(): 
        dict_test[c]= ', '.join(dict_test['entities_discovered'][c])

    return dict_test

The above function does the job. I hope this what you are looking for?

Upvotes: 0

Ajax1234
Ajax1234

Reputation: 71471

Simple recursion using a generator:

def flatten(d):
   for a, b in d.items():
     if isinstance(b, dict):
        yield from flatten(b)
     else:
        yield (a, b if not isinstance(b, list) else ', '.join(map(str, b)))


print(dict(flatten(dict_test)))

Output:

{
 'id': '5d4c2c0fd89234260ec81', 
 'Reference Number': 'JA-L800D-191', 
 'OTHER_ID': 'L800DFAG02191', 
 'CODE_ID': '160472708, 276954773', 
 'label_field': 'ELECTRONICS, HDMI', 
 'numeric_field': '491, 492'
}

Upvotes: 0

chrisckwong821
chrisckwong821

Reputation: 1173

def recursive_flatten_dict(tmp, dict_test):
    for i,v in dict_test.items():
        if type(v) == type({}):
            recursive_flatten_dict(tmp,v)
        else:
            tmp[i] = v
    return tmp

recursive_flatten_dict({},dict_test)

Upvotes: 0

Phoenix
Phoenix

Reputation: 4284

You can use join() built-in method to join values together.

def do():
    dict_test = {
        "id": "5d4c2c0fd89234260ec81",
        "Reference Number": "JA-L800D-191",
        "entities_discovered": {
            "OTHER_ID": [
                "L800DFAG02191"
            ],
            "CODE_ID": [
                "160472708",
                "276954773"
            ]
        },
        "label_field": [
            "ELECTRONICS",
            "HDMI"
        ],
        "numeric_field": [
            491,
            492
        ],
    }

    new_dict = {}
    for key, value in dict_test.items():
        if isinstance(value, dict):
            for _key, _value in value.items():
                if isinstance(_value, list):
                    new_dict.update({_key: ', '.join([str(item) for item in _value])})

        elif isinstance(value, list):
            new_dict.update({key: ', '.join([str(item) for item in value])})

        else:
            new_dict.update({key: value})

    return new_dict


if __name__ == '__main__':
    print(do())

Output:

{
    'id': '5d4c2c0fd89234260ec81',
    'Reference Number': 'JA-L800D-191',
    'OTHER_ID': 'L800DFAG02191',
    'CODE_ID': '160472708, 276954773',
    'label_field': 'ELECTRONICS, HDMI',
    'numeric_field': '491, 492'
}

Upvotes: 1

Related Questions