import json


def build_new_fpl_json(input_json):
    """Given an input FPL dict
    Strip out the non x400message key and add all its subkeys
    as top level keys of a new dict

    Args:
    input_json - dict

    Returns:
    Dict
    """
    new_json = {}
    for key in input_json.keys():
        if key.lower() == 'x400message':
            new_json[key] = json.dumps(input_json[key])
        else:
            for sub_key in input_json[key]:
                new_json[sub_key] = input_json[key][sub_key]
            new_json['MessageType'] = key
    new_json['messageReceievedTime'] = input_json['X400Message']['envelope']['messageDeliveryTime']
    return new_json