Converting Flask form data to JSON only gets first value

request.form is a MultiDict. Iterating over a multidict only returns the first value for each key. To get a dictionary with lists of values, use to_dict(flat=False).

result = request.form.to_dict(flat=False)

All values will be lists, even if there’s only one item, for consistency. If you want to flatten single-value items, you need to process the data manually. Use iterlists with a dict comprehension.

result = {
    key: value[0] if len(value) == 1 else value
    for key, value in request.form.iterlists()
}

Leave a Comment