0

I have a model with JSONField:

class SDReport(models.Model):
    summary = models.JSONField()

summary field data example:

{
    "1": {
        "stm": {
            "1": []
        },
        "non_stm": {
            "1": ["3419250", "3205437"]
        }
    },
    "2": {
        "stm": {
            "1": []
        }
    }
}

How can select data (expected result ["3419250", "3205437"], default value - []) from path '1' > non_stm > '1' with ORM?

UPD. This works:

SDReport.objects.annotate(lst=RawSQL("(summary->'1'->'non_stm'->>'1')", ())).first()

But lst is a string. Is it possible to convert it from string to list in query?

1 Answers1

0

Why can't you treat this as a standard python dictionary?

summary["1"]["non_stm"]["1"] will return: ["3419250", "3205437"]

if it's not a dictionary and just a string of text, try the JSON standard library (import json), first calling: dictionary = JSON.loads(stringVersionOfDictionary)

ThatOneCoder
  • 116
  • 1
  • 5