Uploaded image for project: 'Python Driver'
  1. Python Driver
  2. PYTHON-602

json_util.dumps should preserve field order in SON objects

    • Type: Icon: Improvement Improvement
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 2.7
    • Affects Version/s: 2.6.3
    • Component/s: None
    • Environment:
      OSX 10.9 (or Ubuntu 12.04 x64)
      Python 2.7

      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.

            Assignee:
            jesse@mongodb.com A. Jesse Jiryu Davis
            Reporter:
            chrisdo@vulcan.com Chris Doehring
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: