python - Converting MultiDict to proper JSON -


the multidict (immutablemultidict) receive posting form using flask (python) is

immutablemultidict([     ('disabled', 'false'),      ('disabled', 'false'),      ('debet', '11'),      ('debet', '21'),      ('date', '2016-11-17'),      ('kredit', '12'),      ('kredit', '22'),      ('record', '1901'),      ('record', '1902'),      ('description', 'sales of inventory') ]) 

the form post looks like

<form method="post" action="/post">     <input name="description" value="sales of inventory" />     <input name="date" value="2016-11-17" />     <div class="group">         <input name="record" value="1901" />         <input name="debet" value="11" />         <input name="kredit" value="12" />         <input name="disabled" value="false" />     </div>     <div class="group">         <input name="record" value="1902" />         <input name="debet" value="21" />         <input name="kredit" value="22" />         <input name="disabled" value="false" />     </div> </form> 

i convert form (exclude description , date) input json format looks like

data = [     {         "record": 1901,         "debet": 11,         "kredit": 12,         "disabled": false     },     {         "record": 1902,         "debet": 21,         "kredit": 22,         "disabled": false     } ] 

is there way this? have tried loads of stuff, can't figure out properly.

if absolutely cannot change how data sent, , you're sure data sent in same order form (make sure of this, otherwise there no way tell values go together), can this:

fields = ['record', 'debet', 'kredit', 'disabled']  num_values = len(form_data.getlist(fields[0]))  data = []  in range(num_values):     data.append({field: form_data.getlist(field)[i] field in fields}) 

however, wouldn't recommend this. @ end of day, somehow generating new inputs dynamically, since seems have dynamic number of fields, why not change names record1, record2, etc. when you're doing this?


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -