I would like to convert a POST from Webob MultiDict to nested dictionary. E.g.
So from a POST of:
'name=Kyle&phone.number=1234&phone.type=home&phone.number=5678&phone.type=work'
to a multidict;
[('name', 'Kyle'), ('phone.number', '1234'), ('phone.type', 'home'), ('phone.number', '5678'), ('phone.type', 'work')]
to a nested dictionary
{'name': 'Kyle',
'phone': [
{
'number': '12345',
'type': 'home',
},{
'number': '5678',
'type': 'work',
},
Any ideas?
EDIT
I ended up extracting the variable_decode
method from the formencode package as posted by Will.
The only change that was required is to make the lists explicit, E.g.
'name=Kyle&phone-1.number=1234&phone-1.type=home&phone-2.number=5678&phone-2.type=work'
Which is better for many reasons.
I prefer an explicit way to solve your problem:
Divide the members which belong to the same structure (or dict) into a same group with same field name, like
The order of the fields in the form is guaranteed, so the multidict will be: (('name', 'Kyle'), ('phone1', '1234', 'home'), ('phone2', '5678', 'work'))
Then the code will be like:
If you have formencode installed or can install it, checkout out their variabledecode module
I haven't had the time to test it and it's quite restrictive, but hopefully this will work (I'm only posting because it's been a while since you posted the question):
If this is not 100%, it should at least get you on a good start.
Hope this helps