I have an application where I need to convert Python shelve pickled dictionary files to JSON files.

import ujson, shelve

with open("shelveFile", "r") as sfile:
    shelve_dict=shelve.open(sfile)
    py_dict= dict(shelve_dict)
    with open("jsonfile.json","w") as jsonfile:
        ujson.dump(py_dict, jsonfile)
    with open("jsonfile.json",'r') as readJSONfile:
        ujson.loads(readJSONfile.read())

Note: If I use ujson.load(jsonfile2) I get a serialization error.

The issue I have: the shelve file uses Python tuple datatypes for some of the dictionary keys. I am able to use ujson.dump to save as JSON but when I try to use ujson.load(jsonfile) the keys get loaded as strings and not tuples. Short of using a dictionary comprehension to convert the keys(unsure of that exact syntax), is there a library that would convert a shelve file to a JSON file that I could load back into a Python dictionary object?

When loaded by ujson.loads(fileobj.read()) method:

{"('tuplekey1','tuplekey2')": value,}

Should be:

{('tuplekey1','tuplekey2'): value,} 

(please do not down-vote if question is not clear, I will do my best to clarify if needed... I do not post questions here often.)

1

There are 1 answers

5
AudioBubble On BEST ANSWER

Tuples cannot be used as dictionary keys when you want to save data with shelve or json. So in your case string representation of tuples was used instead of actual tuples - repr(('tuplekey1', 'tuplekey2')) gives "('tuplekey1', 'tuplekey2')".

So you need additional step to parse strings into tuples. For example, eval("('tuplekey1', 'tuplekey2')") or tuple(val.strip("()' ") for val in a.split(',')). Of course, you need to know (or recognize) what strings should be parsed into tuples.

The problem may be avoided - data should stored in another manner - another representation of tuples or another structure of data.