I'd like json_util.dumps to preserve field order when writing SON objects in extended JSON. The special case I'm considering is when I want to write data to a file that is to be imported into a MongoDB database.
Consider these two separate cases:
- I want to create a document value that'll be used in an index
- extended JSON for binary data is required to be a particular order when read by mongoimport
Try this code for example:
from bson.son import SON from bson import json_util doc = {'_id':SON([('b', 1), ('a', 2)]), 'bin_data':Binary(b("\x00\x01\x02"))} print json_util.dumps(doc)
For me, this prints the following:
{"_id": {"a": 2, "b": 1}, "bin_data": {"$type": "00", "$binary": "AAEC"}}
but should be:
{"_id": {"b": 1, "a": 2}, "bin_data": {"$binary": "AAEC", "$type": "00"}}
When I import this data using mongoimport, the data written in the database is:
{ "_id" : { "a" : 2, "b" : 1 }, "bin_data" : { "$type" : "00", "$binary" : "AAEC" } }
when I was hoping for this:
{ "_id" : { "b" : 1, "a" : 2 }, "bin_data" : BinData(0,"AAEC") }
I got around this by post-processing the data before importing it.
I fixed it within json_util in my own environment, but added the dependency on SON. I'll see about contributing that, to see how everyone else fields about that solution.
- is duplicated by
-
PYTHON-625 mongoimport expects specific order of json fields
- Closed