-1

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.

ZedsWhatSheSaid
  • 467
  • 2
  • 9
  • 23
  • 1
    possible duplicate of [Python references](http://stackoverflow.com/questions/2797114/python-references) – simonzack Nov 04 '14 at 10:12
  • Can you guys please as well explain the downvote :) ? I want my question to be as good as possible – ZedsWhatSheSaid Nov 04 '14 at 10:12
  • Your problem is possibly outside the posted code, can you change it to show how your calling build_dict. I mean, are you certain your not calling it with the same parameters? – dpgaspar Nov 04 '14 at 10:16
  • Move `server_dict = {}` *inside* the `for` loop (or just replace the `update` with `server_dict = dict(name=iter_name, url=server_url[i])`). – jonrsharpe Nov 04 '14 at 10:19
  • you saved my life jon... Now that I see that, it is obvious ... I tried to figure it out for 1 whole day now >_> – ZedsWhatSheSaid Nov 04 '14 at 10:22

1 Answers1

1

As jonrsharpe stated, you have your server_dict at the wrong place. You should consider putting it in your for loop, so you just update the two entries which you recently received through your for loop.

if server_name and server_url:
    for i, iter_name in enumerate(server_name):
        server_dict = dict( name = iter_name, url = server_url[i] )
        json_dict['server'].append(server_dict)
BoJack Horseman
  • 4,406
  • 13
  • 38
  • 70