JSON keys order

69 views
Skip to first unread message

yoch....@gmail.com

unread,
Jan 6, 2021, 7:39:35 AM1/6/21
to sqlalchemy
Hi,

I have a problem with a custom JSON type, which is not preserving keys order, despite that I'm using python 3.8.

Here is the type definition and usage:

```
import json
from sqlalchemy import Column, Integer
from sqlalchemy.types import TypeDecorator, TEXT
from sqlalchemy.ext.mutable import MutableDict

class JSONType(TypeDecorator):
    impl = TEXT

    def process_bind_param(self, value, dialect):
        if value is not None:
            value = json.dumps(value)
        return value

    def process_result_value(self, value, dialect):
        if value is not None:
            value = json.loads(value)
        return value

JSONDict = MutableDict.as_mutable(JSONType)

Base = automap_base()

class Picture(Base):
    __tablename__ = 'picture'
    id = Column(Integer, primary_key=True)
    data = Column(JSONDict)
```

When I'm storing values in the data field (values are nested elements of different types), store the picture in database and reload it, the keys order of data dict is not preserved. I'm don't understand why, because the version of python I'm using is supposed to keep the dict insertion order.

Have you some idea how to solve that ? The JSON keys order is important for my application.

Thank you

Mike Bayer

unread,
Jan 6, 2021, 8:31:49 AM1/6/21
to noreply-spamdigest via sqlalchemy
perhaps Python's json encoder does not maintain ordering?   I don't know either.   Can you set echo='debug' on your create_engine() and examine the parameters being passed as well as the data being returned?   also I'm not seeing which database backend or driver you're using which may be significant (particularly if its PostgreSQL which has a fully native JSON datatype).
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
 
 
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.

yoch....@gmail.com

unread,
Jan 6, 2021, 11:10:56 AM1/6/21
to sqlalchemy
Thank you for your answer.

I'm using MySQL 8 with pymysql.

The `data` column has JSON type, and it seems that MySQL is actually reordering keys.

Mike Bayer

unread,
Jan 6, 2021, 12:59:28 PM1/6/21
to noreply-spamdigest via sqlalchemy
Reply all
Reply to author
Forward
0 new messages