I want to take input from an HTML form and give the output in JSON format. When multiple values are selected they are not converted into JSON arrays, only the first value is
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()
}