I have the following Json:
{
file1: {
path_to_file: 'file1.txt',
children : 'file2'
},
file2: {
path_to_file: 'file1.txt',
children : 'file3,file4'
},
file3: {
path_to_file: 'a/file3.txt',
children : ''
},
file4: {
path_to_file: 'b/file4.txt',
children : ''
}
}
I want to construct a tree from this Json. each node should have: name (file1 etc..), path_to_file which is just a data field and convert the children into "pointers" to next node.
I have the following code:
class Node(object):
def __init__(self, name, path_to_file=None):
self.name = name
self.path_to_file= path_to_file
self.children = []
def add_child(self, obj):
self.children.append(obj)
This can be used as:
>>> n = Node(5)
>>> p = Node(6)
>>> q = Node(7)
>>> n.add_child(p)
>>> n.add_child(q)
Now, I want to use properties from my json instead of the number above. So I have this code:
jsonObject= json.load(json_string)
for key in jsonObject:
value = jsonObject[key]
print("The key and value are ({}) = ({})".format(key, value))
This gives me:
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 2 column 4 (char 7)
How can I extract the properties in the Json object inorder to construct the call to the Node class?
json_stringis likely an invalid JSON. Otherwise, our code looks ok-ish. Also,for key, value in jsonObject.items():is more idiomatic.