0

So i have a list of elements:

elements = [room1, room2, room3]

I also have a list of key/value attributes that each room has:

keys = ["level", "finish1", "finish2"]
values = [["ground", "paint1", "carpet1"],["ground", "paint1", "paint2"], ["second level", "paint1", "paint2"]]

is there a way to serialize this two lists into a json file structured like this:

{'room1': [{'level': 'ground', 'finish1': 'paint1', 'finish2': 'carpet1'}],'room2': [{'level': 'ground', 'finish1': 'paint1', 'finish2': 'paint2'}],'room3': [{'level': 'second level', 'finish1': 'paint1', 'finish2': 'paint2'}]}

I am on this weird platform that doesnt support dictionaries so I created a class for them:

class collection():
def __init__(self,name,key,value):
    self.name = name
    self.dict = {}
    self.dict[key] = value
def __str__(self):
    x = str(self.name) + " collection"
    for key,value in self.dict.iteritems():
        x = x + '\n'+ '  %s= %s ' % (key, value)
    return x

then i found a peiece of code that would allow me to create a basic json code from two parallel lists:

def json_list(keys,values):
lst = []
for pn, dn in zip(values, keys):
    d = {}
    d[dn]=pn
    lst.append(d)
return json.dumps(lst)

but this code desnt give me the {room1: [{ ... structure

Any ideas would be great. This software I am working with is based on IronPython2.7

Ok, so the above worked great. I got a great feedback from Comments. I have one more variation that I didnt account for. Sometimes when I try to mix more than singe element type (rooms, columns etc) they might not have the same amount of attributes. For example a room can have (level, finish and finish) while column might have only thickness and material. If i kept it all organized in parallel lists key/value is it possible to modify the definition below:

keys = [[thickness, material],[level,finish,finish]]
values = [[100,paint],[ground,paint,paint]]
elements = [column,room]

How would i need to modify the definition below to make it work? Again I want to export a json file.

konrad
  • 3,544
  • 4
  • 36
  • 75
  • 2
    What do you mean *"doesn't support dictionaries"*? Your `collection` *is implemented using a dictionary*. – jonrsharpe Jan 31 '15 at 17:16
  • Well for some reason when I try and create a dictionary outside of function it throws an error. So I am able to use d = {} inside of the json_list function but not outside of it. – konrad Jan 31 '15 at 17:19
  • What happens if you do `d = dict()` outside? – L3viathan Jan 31 '15 at 17:31
  • if i do d = dict() and then set my OUT = d then OUT prints out Empty List – konrad Jan 31 '15 at 17:34
  • but if i try to do this: names = ["Nick", "Alice", "Kitty"] rating = [1, 2, 3] dictionary_test = dict(zip(names, rating)) it will return an error "'string' does not contain a definition for 'Key'" – konrad Jan 31 '15 at 17:36

2 Answers2

1

I don't know how Python can even work without dictionaries, so please just test this and tell me the error it shows you:

import json

elements = ['r1','r2','r3']
keys = ["level", "finish1", "finish2"]
values = [["ground", "paint1", "carpet1"],["ground", "paint1", "paint2"], ["second level", "paint1", "paint2"]]
d = dict()

for (index, room) in enumerate(elements):
    d[room] = dict()
    for (index2, key) in enumerate(keys):
        d[room][key] = values[index][index2]

print json.dumps(d)
L3viathan
  • 26,748
  • 2
  • 58
  • 81
  • can you have a look at the edit I made to the post. I am not dealing with a list of keys that has an exact same structure like the list of values. I am doing that because my objects might not always have the same amount of attributes so I cant use single attribute list to define them all. Now each object has a list of keys and attributes and they are matching in length. Can you help? – konrad Jan 31 '15 at 19:46
  • Sure. But you need to be consistent. If you're having a structure like you described in your edit, replace the 10th line in my code with `for (index2, key) in enumerate(keys[index]):` – L3viathan Jan 31 '15 at 21:27
1

This may work.

#-*- encoding: utf-8 -*-
import json

elements = ["room1", "room2", "room3"]
keys = ["level", "finish1", "finish2"]
values = [["ground", "paint1", "carpet1"],["ground", "paint1", "paint2"], ["second level", "paint1", "paint2"]]

what_i_want = dict((room, [dict(zip(keys, value))])
                   for room, value in zip(elements, values))
print(json.dumps(what_i_want))
dgoon
  • 11
  • 2