How to Convert to dict in django data from ajax

Need to convert this data to list of dict in django

<QueryDict: {'data[0][val]': ['0.00'], 'data[0][id]': ['e82c2454-7d0f-4af2-90a8-58998e2e4780'], 'data[0][modal]': ['appointment'], 'data[1][val]': ['0.00'], 'data[1][id]': ['e82c2454-7d0f-4af2-90a8-58998e2e4780'], 'data[1][modal]': ['appointment'], 'data[2][val]': ['0.00'], 'data[2][id]': ['05b796cf-27de-42e0-b5ef-59ba5e8aeb0a'], 'data[2][modal]': ['additional_study'], 'data[3][val]': ['0.00'], 'data[3][id]': ['05b796cf-27de-42e0-b5ef-59ba5e8aeb0a'], 'data[3][modal]': ['additional_study']}>

To

Need to convert to

[
{
'val':'',
'id':'dfsdf'
},
{
'val':'',
'id':'dfsdf'
}
]

In the way you are passing data (all keys are different), there is no QueryDict built-in function that can help directly. Since you have three keys (val, id, modal) you could build the object in the following way:

qd_list = list(query_dict.values())

dict_list = [{'val': qd_list[i][0], 'id': qd_list[i+1][0]} for i in range(0, len(list(query_dict.values())), 3)]

If you also want modal key then:

{'val': qd_list[i][0], 'id': qd_list[i+1][0], 'modal': qd_list[i+2][0]} 
Back to Top