I am writing some code where my highest aim is, to deliver some information in json format. I want to display servernames and serverurls like this: ( My aim is not the pretty print but I just displayed it like this, so it is more obvious )
{
"server": [
{ "name" : "some_server_name", "url" : "some_server_url" }
{ "name" : "another_server_name", "url" : "another_server_url" }
]
}
I got now two lists, one with the servernames and the other with serverulrs. I iterate over the names and then access to the matching url. After that is done I want to append it to a dictionary. When I got this dictionary. I want to append this to my json_dict. I created that dict because I have two other lists which I want to display too in my json information. ( You can ignore rdp and cert because they are already displayed correctly )
def build_dict(server_name, server_url, rdp, cert):
json_dict = defaultdict(list)
server_dict = {}
if server_name and server_url:
for i, iter_name in enumerate(server_name):
server_dict.update( name = iter_name, url = server_url[i] )
json_dict['server'].append(server_dict)
if rdp:
for iter_rdp in rdp:
json_dict['config'].append(iter_rdp)
if cert:
for iter_cert in cert:
json_dict['cert'].append(iter_cert)
return json_dict
The entries of my servernames list:
[u'123name', u'qwer123']
And the entries of my serverurls list:
[u'123url', u'qwer123']
Unfortunately the final output looks like this:
{"cert": ["123url", "123123", "123123123"],
"config": ["123rdp", "123123", "123123123"],
"server": [
{"url": "qwer123", "name": "qwer123"},
{"url": "qwer123", "name": "qwer123"}
]
}
You can ignore cert and config because this already works as expected. I just don't understand why I have:
{"url": "qwer123", "name": "qwer123"},
{"url": "qwer123", "name": "qwer123"}
And not:
{"url": "123name", "name": "123url"},
{"url": "qwer123", "name": "qwer123"}
as output.