how to represent postgresql json field in Ninja schema

Hello. CAn someone suggest me a solution for this.
I guess I am missing something very silly.
I have a postgresql table with a json field which is perfectly represented in my django model.
But I can’t find a way to represent it in my ninja schema.
class orgin(Schema):
orgname: str
orgaddr: str
orgdetails: json #does not work.

Show properly your full code in code blocks. Where are your imports?

All right here’s the entire code.

from ninja import Schema
class orgin(Schema):
orgname: str
orgaddr: str
orgdetails: json

So the last field represents a json field which is correctly mapped to the django model.

Am I missing any imports here?

Regards.

What is the output of orgdetails.json()?

You could do something like this:

import json

class orgin(Schema):
  orgname: str
  orgaddr: str
  orgdetails: list

  @staticmethod
  def resolve_orgdetails(obj):
    json_list = json.loads(obj.orgdetails)
    
    return json_list

If you know exactly how the json data looks, and the datatypes are the same for every row, you could define a schema for that data like shown below. The auto generated documentation will look more complete this way.

from typing import List

class JsondataOut(Schema):
  key_1: str
  key_2: str
  key_3: bool

class orgin(Schema):
  orgname: str
  orgaddr: str
  orgdetails: List[JsondataOut]

  @staticmethod
  def resolve_orgdetails(obj):
    json_list = json.loads(obj.orgdetails)

    return json_list

If you do not know the datatype of a specific key, you could also use Any like this:

from typing import List, Any

class JsondataOut(Schema):
  key_1: str
  key_2: str
  key_3: Any

Reference: