Reputation: 1204
Suppose I'm dealing with the following two (or more) JSON strings from a dictionary:
JSONdict['context'] = '{"Context":"{context}","PID":"{PID}"}'
JSONdict['RDFchildren'] = '{"results":[ {"object" : "info:fedora/book:fullbook"} ,{"object" : "info:fedora/book:images"} ,{"object" : "info:fedora/book:HTML"} ,{"object" : "info:fedora/book:altoXML"} ,{"object" : "info:fedora/book:thumbs"} ,{"object" : "info:fedora/book:originals"} ]}'
I would like to create a merged JSON string, with "context" and "query" as root level keys. Something like this:
{"context": {"PID": "wayne:campbellamericansalvage", "Context": "object_page"}, "RDFchildren": {"results": [{"object": "info:fedora/book:fullbook"}, {"object": "info:fedora/book:images"}, {"object": "info:fedora/book:HTML"}, {"object": "info:fedora/book:altoXML"}, {"object": "info:fedora/book:thumbs"}, {"object": "info:fedora/book:originals"}]}}
The following works, but I'd like to avoid using eval()
if possible.
# using eval
JSONevaluated = {}
for each in JSONdict:
JSONevaluated[each] = eval(JSONdict[each])
JSONpackage = json.dumps(JSONevaluated)
Also got this way working, but feels hackish and I'm afraid encoding and escaping will become problematic as more realistic metadata comes through:
#iterate through dictionary, unpack strings and concatenate
concatList = []
for key in JSONdict:
tempstring = JSONdict[key][1:-1] #removes brackets
concatList.append(tempstring)
JSONpackage = ",".join(concatList) #comma delimits
JSONpackage = "{"+JSONpackage+"}" #adds brackets for well-formed JSON
Any thoughts? advice?
Upvotes: 0
Views: 127