# postgresql jsonField save dict

**URL:** <https://forum.djangoproject.com/t/postgresql-jsonfield-save-dict/28839>\
**Category:** Using the ORM\
**Created:** [March 8, 2024, 1:10am UTC](https://forum.djangoproject.com/t/postgresql-jsonfield-save-dict/28839 "2024-03-08T01:10:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![CodePythonFollow](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/codepythonfollow/32/19709_2.png) [@CodePythonFollow](https://forum.djangoproject.com/u/CodePythonFollow)\
**Post date:** [March 8, 2024, 1:10am UTC](https://forum.djangoproject.com/t/postgresql-jsonfield-save-dict/28839/1 "2024-03-08T01:10:25Z")

</div>

```auto
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
         ...

```

DBMS: PostgreSQL (Version 14.4 (Ubuntu 14.4-1.pgdg20.04+1))  
Case sensitive: normal form=lower, delimited form=exact  
Driver: PostgreSQL JDBC Driver (version 42.2.5, JDBC4.2)

postgresql jsonfield save data like it  
{“a”: “b”}

jango default jsonField get data is dict

 ![image](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/4/7/478615a9a75beb64ca7f7f2f515dd4ceb8942a9b.png)

This is how I currently solve it.

```auto
class CustomJsonField(models.JSONField):
    def from_db_value(self, value, expression, connection):
        if isinstance(value, dict):
            return value
        return super().from_db_value(value, expression, connection)

```

Is it possible to consider solving it this way?

```diff
        if isinstance(expression, KeyTransform) and not isinstance(value, str):
            return value
        try:
            # PostgreSQL database save json return dict
+ if isinstance(value, dict):
+ return value
            return json.loads(value, cls=self.decoder)
        except json.JSONDecodeError:
            return value

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 8, 2024, 1:18am UTC](https://forum.djangoproject.com/t/postgresql-jsonfield-save-dict/28839/2 "2024-03-08T01:18:19Z")

</div>

There’s not much of a description here of the issue you’re facing.

Can you provide more details about what the problem is that you’re having and are trying to solve?

---

<div class="post-metadata">

**Author:** ![CodePythonFollow](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/codepythonfollow/32/19709_2.png) [@CodePythonFollow](https://forum.djangoproject.com/u/CodePythonFollow)\
**Post date:** [March 8, 2024, 8:43am UTC](https://forum.djangoproject.com/t/postgresql-jsonfield-save-dict/28839/3 "2024-03-08T08:43:18Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/6/0/60f1dd62d949cf3e907412ad34907a41bec1b182.png)

My solution was to inherit JSONField

```python
class CustomJsonField(models.JSONField):
    def from_db_value(self, value, expression, connection):
        if isinstance(value, dict):
            return value
        return super().from_db_value(value, expression, connection)

```
