How to store/read deeply nested JSON objects?

I am trying store deeply nested JSOn objects (in my case these are Kubernetes YAMLs) like tasty-kube/busy-dep.yaml at master · appscodelabs/tasty-kube · GitHub in dgraph and run search on them. For example, I want to list metdata.name of all kind: Deployment that uses spec.template.spec.containers[*],name == busybox .

I am new to dgraph. Can you please help me understand how something like this can be accomplished?

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