How to store/read deeply nested JSON objects?

Hi @tamalsaha,

Welcome to the dgraph community :slight_smile:

Dgraph supports two ways for mutation, the rdf way and the json way. You can opt any one of them. If you have a nested JSON, you can straight away use it in the mutation and dgraph will figure out the schema accordingly.

An example mutation for your use case would be something like below:

 {
    "set": [
      {
        "kind": "deployment",
        "metadata": {
          "name": "apple",
          "labels": {
            "app": "busy-dep"
          }
        },
        "spec": {
          "template": {
            "spec":{
              "container":[
                  {
                    "name":"sandbox"
                  },
                  {
                    "name" :"busybox"
                  }
              ]
            }
          }
        }
        },
        {
        "kind": "deployment",
        "metadata": {
          "name": "apple",
          "labels": {
            "app": "busy-dep"
          }
        },
        "spec": {
          "template": {
            "spec":{
              "container":[
                {
                  "name":"other"
                },
                {
                  "name" :"something"
                }
              ]
            }
          }
        }
      }
    ]
}

This can be done by the following query:

{
  q(func: eq(kind, "deployment")) @cascade{
      uid
      metadata{
        name
      }
      spec {
        template {
          spec {
            container @filter(eq(name, "busybox"))
          }
        }
      }
  }
}

Feel free to ask followup questions.

2 Likes