[Fixed]-Keeping json data in the order it was inserted – django

13πŸ‘

If you need to maintain key order in a JSONFIeld you can override the db_type for the native JSONField to use json rather than jsonb:

class RawJSONField(JSONField):

    def db_type(self, connection):
        return 'json'

From the Postgres documentation:

The major practical difference is one of efficiency. The json data type stores an exact copy of the input text, which processing functions must reparse on each execution; while jsonb data is stored in a decomposed binary format that makes it slightly slower to input due to added conversion overhead, but significantly faster to process, since no reparsing is needed. jsonb also supports indexing, which can be a significant advantage.

If you don’t need to index your data based on the contents of the JSON blob and really do need to maintain the key order that was created when you instantianted your dictionary (perhaps you are using a client side library that makes assumption on JSON key order), then this might work for you.

πŸ‘€RedBlueThing

2πŸ‘

this is interesting question, especially because there is no guarantee about order of fields in the javascript object, JSON itself or python dict

taking above into consideration, if we will get our order set properly in some combination of database + serialization + deserialization + presentation it will work only for this (quite narrow) configuration

e.g. django-jsonfield serializes (by default) JSON to the text field, so on the database level it may be safe, but deserialization from few json modules (json, simplejson, ujson, yajl) is done to dictionary and then all order is lost

django 1.9 introduces native postgresql JSONField – and here you do not have guarantee even on the database level, because data is stored internally as jsonb – binary optimized JSON

in my opinion, instead of searching for serialization to OrderedDict, better to change how data is stored, if order is important – let’s store data in the table:

[{date: "05/2013", price: 101},
 {date: "04/2013", price: 100.9},
 {date: "03/2013", price: 100.5},
 {date: "02/2013", price: 100.3},
 {date: "01/2013", price: 100.3}]

We have guarantee of the order, data can be processed in linear matter and we are not depending on special implementations.

πŸ‘€Jerzyk

Leave a comment