I am seeking to use a Python dictionary with key: array pairs as a basis for constructing a database table. The dictionary is constructed as follows:
schema = {'names': ['Field1', 'Field2', 'Field3'], 'type': ['STRING', 'STRING', 'INTEGER']}
The resulting JSON data structure that I am seeking to produce would be, for lack of better phrasing, an array of JSON elements, as below:
[
{
'names': 'Field1',
'type': 'STRING'
},
{
'names': 'Field2',
'type': 'STRING'
},
{
'names': 'Field3',
'type': 'INTEGER'
}
]
I have used the json module and attempted to do this as follows:
import json
schema = {'names': ['Field1', 'Field2', 'Field3'], 'type': ['STRING', 'STRING', 'INTEGER']}
json.dumps(schema)
However, it gives me:
'{"names": ["Field1", "Field2", "Field3"], "type": ["STRING", "STRING", "INTEGER"]}'
I can do this by constructing the string using {}.format(..) but I wanted to see if there was a more efficient manner. I have searched through numerous threads but most threads seem to do the opposite of what I am seeking to do.
Any pointers?
Cheers.