Kraken18
Kraken18

Reputation: 673

dynamic dictionary to JSON problem

I'm currently working on a project using javascript and python with Jquery Datatables plugin and Django. My problem is that I am trying to create a dictionary in Python which I then wish to perform a json.dumps(dictionary) in order to send it across to the JQuery.

The conversion is performed and I can using a debugger see the data in the JQuery/Javascript code however it does not populate my table the reason being that the JSON is malformed. This I believe is due to the way the Dictionary in python code is created. I know I'm doing something wrong at this step. All the examples I've seen show a hard coded dictionary I need to create my dictionary dynamically. Here is the code I'm using in python. If anyone knows what I'm doing wrong please let me know this is driving me insane. This is the latest permutation of the dictionary code:-

    dictionary = {}
    array = []
    status = 'OFF'
    for item in self.scanResults:
        if item[6]:
            status = 'ON'
        else:
            status = 'OFF'

        array.append( {'MAC_ADDRESS':item['mac_addr'],
                    'IP_ADDRESS':item['ip_addr'],
                    'NAME':item['name'],
                    'OS':item['os'],
                    'OS_VERSION':item['os_version'],
                    'WORKGROUP':'--',
                    'STATUS':status
        })
    dictionary = dict({'aaData': array})    

The JSON on the JQuery side should be in this format:-

{"aaData": [[..,..,..,..,],[..,..,..,..]]}

Cheers for any help you can provide

Chris

EDIT:

Additional information. Sorry for not including it earlier. I'm encoding it the following way :-

return HttpResponse(simplejson.dumps(response_dict), mimetype='application/javascript')

The screen dump of results is:-

{"aaData": [{"STATUS": "ON", "WORKGROUP": "--", "IP_ADDRESS": "192.168.0.2", "OS_VERSION": "8.04", "MAC_ADDRESS": "00:10:e3:42:16:35", "OS": "Linux", "NAME": "Machine_One"}, {"STATUS": "ON", "WORKGROUP": "--", "IP_ADDRESS": "192.168.0.3", "OS_VERSION": "8.04", "MAC_ADDRESS": "00:19:a3:41:16:31", "OS": "Linux", "NAME": "Machine_Two"}, {"STATUS": "ON", "WORKGROUP": "--", "IP_ADDRESS": "192.168.0.4", "OS_VERSION": "8.04", "MAC_ADDRESS": "00:19:b3:43:16:32", "OS": "Linux", "NAME": "Machine_Three"}, {"STATUS": "ON", "WORKGROUP": "--", "IP_ADDRESS": "192.168.0.5", "OS_VERSION": "8.04", "MAC_ADDRESS": "00:19:c3:44:16:33", "OS": "Linux", "NAME": "Machine_Four"}, {"STATUS": "ON", "WORKGROUP": "--", "IP_ADDRESS": "192.168.0.6", "OS_VERSION": "8.04", "MAC_ADDRESS": "00:19:d3:45:16:34", "OS": "Linux", "NAME": "Machine_Five"}, {"STATUS": "ON", "WORKGROUP": "--", "IP_ADDRESS": "192.168.0.7", "OS_VERSION": "8.04", "MAC_ADDRESS": "00:19:e3:46:16:37", "OS": "Linux", "NAME": "Machine_Six"}, {"STATUS": "ON", "WORKGROUP": "--", "IP_ADDRESS": "192.168.0.8", "OS_VERSION": "8.04", "MAC_ADDRESS": "00:19:f3:47:16:38", "OS": "Linux", "NAME": "Machine_Seven"}, {"STATUS": "ON", "WORKGROUP": "--", "IP_ADDRESS": "192.168.0.9", "OS_VERSION": "8.04", "MAC_ADDRESS": "00:19:g3:48:16:38", "OS": "Linux", "NAME": "Machine_Eight"}, {"STATUS": "ON", "WORKGROUP": "--", "IP_ADDRESS": "192.168.0.10", "OS_VERSION": "8.04", "MAC_ADDRESS": "00:19:h3:49:16:41", "OS": "Linux", "NAME": "Machine_Nine"}]}

The actual epected results would be :-

{"aaData":[["ON","--","192.168.0.6", "8.04", "00:19:d3:45:16:34", "Linux", "Machine_Five"], ["ON","--","192.168.0.6", "8.04", "00:19:d3:45:16:34", "Linux", "Machine_Five"]]} Just to give you an idea.

I'm using jQuery DataTables plugin for this. I beleive the problem is malformed dictionary ie: my fault. I'm new to python and have tried numerous permutations of dictionaries and have discovered 10+ ways not to do it I just need a hint to that elusive right way.

Cheers again

Chris

SOLVED:

I've solved the issue by passing back from the server an array of arrays (list of lists) if you prefer and parsing them on the client side due to the fixed size of the lists this is not causing a performance issue. I shall look more closely at this at a later date and post up a better fix or more elegant solution.

Upvotes: 0

Views: 1830

Answers (4)

Narendra Kamma
Narendra Kamma

Reputation: 1441


array.append([item['mac_addr'], # note: we are appending array.
              item['ip_addr'],
              item['name'],
              item['os'],
              item['os_version'],
              '--',
              status
            ])

when you serialize this, you get data in following format.
[["xxx","yyy","zzz"],["xx1","yy1","zz1"]]
put that in dictionary, dictionary = {'aaData': array}

Upvotes: 0

John Machin
John Machin

Reputation: 82934

The "expected data" appears to be FIVE items in a LIST (in a rather unexpected i.e. scrambled order); you are stuffing SEVEN items into a DICT -- WHY??

Take 2 (taking your unannounced edit into account):

The "expected data" appears to be seven in a LIST (in a rather unexpected i.e. scrambled order); you are stuffing seven key:value items into a DICT -- WHY??

Are you sure that the expected data list should be in that scrambled order?

Upvotes: 0

Andrew Wilkinson
Andrew Wilkinson

Reputation: 10846

The code in your question does not contain the key bit we need to see - how you are serialising your dictionary into a string. If you are just doing str(dictionary) then that will not work, you need to encoded it using simplejson. You can install this library with easy_install, or if you're using Python 2.6+ then it's included as json.

To encode a Python object as a JSON string simple use json.dumps(dictionary).

You say that your Javascript code is expecting an object containing nested lists, but your Python code seems to be generating an object like {"aaData": [{..:.., ..:..},{..:.., ..:..}]}, i.e. a dictionary, inside a list, inside a dictionary. If the problem isn't with how you're encoding the JSON string can be clearer about the object you're expecting?

EDIT

As Ignacio Vazquez-Abrams states in your Python code you're adding dictionaries to the list then expecting them to have magically transformed into lists when they're encoded as JSON. You either need to convert your Javascript to work with the data you are sending at the moment, or replace your current Python code with something similar to what Ignacio suggests.

I would suggest the first of these options as it is easy to get things wrong when using a list to represent a structure and you'll end up using the wrong value because you got the indexes wrong. If you keep it as a dictionary/object then you can access things by name and you avoid that problem.

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798676

If it's malformed then you're malforming it. Did you mean:

    array.append([item['mac_addr'],
                item['ip_addr'],
                item['name'],
                item['os'],
                item['os_version'],
                '--',
                status
    ])

Upvotes: 2

Related Questions