I am trying to serialize Python objects to JSON using json.dumps. If you serialize dictwith json.dumps, it will obviously be serialized as a JSON dictionary {..}; if you serialize listor tuple, it will be a JSON array.
I want to know if there is a way to easily serialize Python dictas JSON list, if possible. If possible, I mean if the keys start at 0 and are ordered, for example:
{0:'data',1:'data',2:'data}
The above will be serialized in JSON as: '{"0": "data", "1": "data", "2": "data"}'but I would like it to be serialized as ['data','data','data'], since the keys start at 0 and are ordered.
My reasoning is that I have a lot of JSON data that is serialized from PHP, where there are keys in PHP arrays, and if the keys are sequenced as described above, PHP json.encodeuses arrays if they are entered in any other ways, they are serialized like dictionaries JSON I want my JSON serializations to match both my PHP and Python code. Unfortunately, changing PHP code is not an option in my case.
Any suggestions? The only solution I found was to write my own function to go through and check each python dictionary and see if it can be converted to listbefore first json.dumps.
EDIT . This object that I am serializing can be listor dict, it can also have additional dict inside, lists, etc. (nesting). I am wondering if there is any “simple” way to do this, otherwise I believe that I can write a recursive solution myself. But it is always better to use existing code to avoid errors.