I would like to convert flat form data to recursive JSON data in python or javascript. This JSON data can later be interpreted by a template engine (google for tempest, it has django like syntax). There are plenty examples to convert flat data to recursive data, but the problem is it can't be a dict or list only.
I tried to do it in many ways, but didn't succeed yet. So after scratching my head for at least two weeks, I decided to ask a question here.
The formdata is like this (key names may be different):
formdata = [
{"formname": "name", "formvalue": "Roel Kramer"},
{"formname": "email", "formvalue": "[email protected]"},
{"formname": "paragraph-0.title", "formvalue": "test titel 1"},
{"formname": "paragraph-0.body", "formvalue": "bla bla body 1"},
{"formname": "paragraph-0.image-0.src", "formvalue": "src 1"},
{"formname": "paragraph-0.image-1.src", "formvalue": "src 2"},
{"formname": "paragraph-1.title", "formvalue": "test titel 2"},
{"formname": "paragraph-1.body", "formvalue": "bla bla body 2"},
{"formname": "paragraph-1.image-0.src", "formvalue": "src 3"},
{"formname": "paragraph-1.image-1.src", "formvalue": "src 4"},
{"formname": "paragraph-1.image-2.src", "formvalue": "src 5"},
{"formname": "paragraph-2.title", "formvalue": "test titel 3"},
{"formname": "paragraph-2.body", "formvalue": "bla bla body 3"},
{"formname": "paragraph-2.image-0.src", "formvalue": "src 6"},
{"formname": "paragraph-2.image-1.src", "formvalue": "src 7"},
]
I would like to convert it to this format:
{'paragraph':
[
{
'image': [{'src': 'src 1'}, {'src': 'src 2'}],
'body': 'body 2',
'title': 'titel 2'
},
{
'image': [{'src': 'src 3'}, {'src': 'src 4'}, {'src': 'src 5'}],
'body': 'body 2',
'title': 'titel 2'
},
{
'image': [{'src': 'src 6'}, {'src': 'src 7'},
'body': 'body 3',
'title': 'titel 3'
},
],
}
As you can see I mix dicts with lists, which makes it a bit harder. In my last attempt I got to the point where the script figures out where to add lists and where to add dicts. This results in this:
{'paragraph': [{'image': []}, {'image': []}, {'image': []}]}
But when I add data the result is not what I expected.
{"paragraph": [{"body": "bla bla body 1", "image": {"src": "src 7"}, "title": "test titel 1"}, {"body": "bla bla body 2", "image": {"src": "src 5"}, "title": "test titel 2"}, {"body": "bla bla body 3", "image": {"src": "src 3"}, "title": "test titel 3"}, {"image": {"src": "src 6"}}], "name": "Roel Kramer", "email": "[email protected]"}
The total script can be seen at github gist. I know it can be much cleaner, but I will refactor it when it works.
What am I doing wrong? Am I totally missing something? Thanks a lot!