简体   繁体   中英

Nested JSON to Flattened JSON using Python

My JSON file looks like this -

sample4 = {
        "a": 1,
        "b": 2, 
        "c": 3,
        "d": [{"a": 5, "b": 6}, {"a": 7, "b": 8}],
        "e": [{"a": 1}, {"a": 2}],
        "f": 9,
        "g": [{"a": 5, "b": 6}, {"a": 7, "b": 8}]
    }

The code that I have used for flattening JSON is -

def flatten_json(y):
    out = {}

def no_mas(x, name=''):
    out[name[:-1]] = x

def flatten(x, name=''):
    if type(x) is dict:
        for a in x:
            if a == 'MetaDataList':
                no_mas(x[a], name + a + '_')
            else:
                flatten(x[a], name + a + '_')
    elif type(x) is list:
        i = 0
        for a in x:
            flatten(a, name)
            i += 1
    else:
        out[name[:-1]] = x

    flatten(y)
    return out

This is the output that I am getting -

在此处输入图像描述

But I am looking for this output -

在此处输入图像描述

To achieve this you first need to calculate how much steps you have to do.

Please test it because it is a very complex task i'm not 100% sure if it suites your needs.

sample4 = {
        "a": 1,
        "b": 2, 
        "c": 3,
        "d": [{"a": 5, "b": 6}, {"a": 7, "b": 8}],
        "e": [{"a": 1}, {"a": 2}],
        "f": 9,
        "g": [{"a": 5, "b": 6}, {"a": 7, "b": 8}]
    }

def count_steps(dictionary):
    """counts the needed steps from the longest list inside the dictionary"""
    return max((len(value) for value in dictionary.values() if isinstance(value, list)))

def flatten(dictionary, name=''):
    steps = count_steps(dictionary)
    return_out = []
    for step in range(0, steps):
        out = {}
        for key, value in dictionary.items():
            if isinstance(value, list):
                for key_inner, value_inner in value[step].items():
                    combined_key = key + '_' + key_inner
                    if combined_key not in out:
                        out[combined_key] = []
                    out[combined_key] = value_inner
            else:
                out[key] = value
        return_out.append(out)
    return return_out

print(flatten(sample4))

#[
# {'a': 1, 'b': 2, 'c': 3, 'd_a': 5, 'd_b': 6, 'e_a': 1, 'f': 9, 'g_a': 5, 'g_b': 6},
# {'a': 1, 'b': 2, 'c': 3, 'd_a': 7, 'd_b': 8, 'e_a': 2, 'f': 9, 'g_a': 7, 'g_b': 8}
#]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM