def flatten_json(
    form,
    json,
    parent_key='',
    separator='-',
    skip_unknown_keys=True
):
    """Flattens given JSON dict to cope with WTForms dict structure.

    :form form: WTForms Form object
    :param json: json to be converted into flat WTForms style dict
    :param parent_key: this argument is used internally be recursive calls
    :param separator: default separator
    :param skip_unknown_keys:
        if True unknown keys will be skipped, if False throws InvalidData
        exception whenever unknown key is encountered

    Examples::

        >>> flatten_json(MyForm, {'a': {'b': 'c'}})
        {'a-b': 'c'}
    """
    if not isinstance(json, collections.Mapping):
        raise InvalidData(
            u'This function only accepts dict-like data structures.'
        )

    items = []
    for key, value in json.items():
        try:
            unbound_field = getattr(form, key)
        except __HOLE__:
            if skip_unknown_keys:
                continue
            else:
                raise InvalidData(u"Unknown field name '%s'." % key)

        try:
            field_class = unbound_field.field_class
        except AttributeError:
            if skip_unknown_keys:
                continue
            else:
                raise InvalidData(u"Key '%s' is not valid field class." % key)

        new_key = parent_key + separator + key if parent_key else key
        if isinstance(value, collections.MutableMapping):
            if issubclass(field_class, FormField):
                nested_form_class = unbound_field.bind(Form(), '').form_class
                items.extend(
                    flatten_json(nested_form_class, value, new_key)
                    .items()
                )
            else:
                items.append((new_key, value))
        elif isinstance(value, list):
            if issubclass(field_class, FieldList):
                nested_unbound_field = unbound_field.bind(
                    Form(),
                    ''
                ).unbound_field
                items.extend(
                    flatten_json_list(
                        nested_unbound_field,
                        value,
                        new_key,
                        separator
                    )
                )
            else:
                items.append((new_key, value))
        else:
            items.append((new_key, value))
    return dict(items)
dataset/ETHPy150Open kvesteri/wtforms-json/wtforms_json/__init__.py/flatten_json