# How to access double nested data in Kibana/Vega

**URL:** <https://discuss.elastic.co/t/how-to-access-double-nested-data-in-kibana-vega/308806>\
**Category:** Kibana\
**Tags:** vega\
**Created:** [July 4, 2022, 10:23am UTC](https://discuss.elastic.co/t/how-to-access-double-nested-data-in-kibana-vega/308806 "2022-07-04T10:23:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![InesCM](https://avatars.discourse-cdn.com/v4/letter/i/9d8465/32.png) [@InesCM](https://discuss.elastic.co/u/InesCM)\
**Post date:** [July 4, 2022, 10:23am UTC](https://discuss.elastic.co/t/how-to-access-double-nested-data-in-kibana-vega/308806/1 "2022-07-04T10:23:44Z")

</div>

Hi! I'm trying to display an stacked bar with information in a double nested array in Vega.  
I know that people struggle with this and I've read other questions/responses, but I still cannot get this right.

This is the response that I have from ES. I need to display the frequencies, and for each frequency bar, stack the amount of responses per range:

```auto
"aggregations" : {
    "freqs" : {
      "doc_count" : 1401908921,
      "freq" : {
        "doc_count_error_upper_bound" : 16628988,
        "sum_other_doc_count" : 804305015,
        "buckets" : [
          {
            "key" : 314000.0,
            "doc_count" : 59767102,
            "rangos" : {
              "buckets" : [
                {
                  "key" : "1.0-29.0",
                  "from" : 1.0,
                  "to" : 29.0,
                  "doc_count" : 104
                },
                {
                  "key" : "30.0-34.0",
                  "from" : 30.0,
                  "to" : 34.0,
                  "doc_count" : 681
                },
                {
                  "key" : "34.0-*",
                  "from" : 34.0,
                  "doc_count" : 160348
                }
              ]
            }
          },
          {
            "key" : 266000.0,
            "doc_count" : 59764524,
            "rangos" : {
              "buckets" : [
                {
                  "key" : "1.0-29.0",
                  "from" : 1.0,
                  "to" : 29.0,
                  "doc_count" : 509
                },
                {
                  "key" : "30.0-34.0",
                  "from" : 30.0,
                  "to" : 34.0,
                  "doc_count" : 3201
                },
                {
                  "key" : "34.0-*",
                  "from" : 34.0,
                  "doc_count" : 1281864
                }
              ]
            }

```

So far I understand that the "property" of the format does not accept double nesting, so `property: aggregations.freqs.freq.buckets` works, but `property: aggregations.freqs.freq.buckets.rangos.buckets` does not work.

I've tried to do this with a project type such as this:

```auto
format: {
      property: aggregations.freqs.freq.buckets
    }
  }
  transform: [
    {
      type: project
      fields: [
        key
        rangos.buckets.key
        rangos.buckets.doc_count
      ]
      as: [
        frequencies
        ranges
        devices
      ]
    }
  ]

```

But somehow I get "undefined", so I'm clearly not doing this right.

Thanks for your support!

---

<div class="post-metadata">

**Author:** ![bhavyarm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bhavyarm/32/22392_2.png) [@bhavyarm](https://discuss.elastic.co/u/bhavyarm)\
**Post date:** [July 5, 2022, 4:54pm UTC](https://discuss.elastic.co/t/how-to-access-double-nested-data-in-kibana-vega/308806/2 "2022-07-05T16:54:10Z")

</div>

@nyuriks / @flash1293 can we please get some help?

Thanks,  
Bhavya

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [July 6, 2022, 7:01am UTC](https://discuss.elastic.co/t/how-to-access-double-nested-data-in-kibana-vega/308806/3 "2022-07-06T07:01:07Z")

</div>

With `format: { property: aggregations.freqs.freq.buckets }`,

I suppose you may need [flatten transform](https://vega.github.io/vega/docs/transforms/flatten/) for `rangos.buckets` field to flatten the nested buckets.

To debug vega scripts, "Inspect" page will help you. You can see the intermediate results

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/c/b/cba716c0d59edcef38b32f4612050fff28ca0385.png)

---

<div class="post-metadata">

**Author:** ![InesCM](https://avatars.discourse-cdn.com/v4/letter/i/9d8465/32.png) [@InesCM](https://discuss.elastic.co/u/InesCM)\
**Post date:** [July 12, 2022, 10:14am UTC](https://discuss.elastic.co/t/how-to-access-double-nested-data-in-kibana-vega/308806/4 "2022-07-12T10:14:48Z")

</div>

Hi!  
I'm trying the flatten transform, but still get undefined for the rangos.buckets.doc\_count ☹  
I'm trying this:

```auto
 transform: [
    {
      flatten: [
        rangos.buckets
      ]
    }
    {
      calculate: datum.key
      as: frequencies
    }
    {
      calculate: datum.doc_count
      as: devices
    }
  ]
  mark: bar
  encoding: {
    x: {
      field: frequencies
      type: nominal
      axis: {
        title: false
      }
    }
    y: {
      field: devices
      type: quantitative
      axis: {
        title: Count
      }
    }
    color: {
      field: rangos.buckets.doc_count
      type: nominal
    }
  }

```

I'm using the inspect option for Vega but I'm unable to see what's being flattened and mapped, so I'm kind of blind on what's going wrong...

Am I using the flatten transformr right? Is there any way to see how it's being flattened, or why it's not getting the right field?

Thanks!

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [July 13, 2022, 10:10am UTC](https://discuss.elastic.co/t/how-to-access-double-nested-data-in-kibana-vega/308806/5 "2022-07-13T10:10:10Z")

</div>

To see what I tried,

> **[Editor/IDE for Vega and Vega-Lite](https://vega.github.io/editor/#/url/vega-lite/N4IgJghgLhIFygG4QDYFcCmBneBtUA1hgJ7wDMAjACwAMdANOAPYDGA+i02gHZTwCsATgDsANmEUaAJkYAnCNwDmTHAhAAjNCyJRV+EEVJwQFAHQ0AtFMHmQjAGaymAW3gVGUJvGuMwrDly8bjRUAL70hCTwIGQ05hZkVLYOTq5wsR5e6VS+-pw8fHCiABwU4ZFGMUmWAFR2II4u5DnM7PlBcBSiNInFoQC6oeUGUXBSot0MrQEFAiKiVPxSLfJKKvCgmtoYungV0WaW1skNqW6Z3oK5bYGF-DSCw4bRsfGJJ41pGSCezdczHTIUhoZQiI0q71q9U+f2m7UKFCkpWKCwGQ0GHlWWHsTFkaX09hQ0CgGG4eBAq2UWFMWx0OEG-UYzggsgI0QADkwAJZBRikzhgHmKDYgAAeIvsXIwKDA0UpKhpWjpphh4RARlAkulsuM8uptJ21N+Q1CQA)**
>
> The Vega editor is a web application for authoring and testing Vega and Vega-Lite visualizations. It includes a number of example specifications that showcase both the visual encodings and interaction techniques.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/2/a/2a90610350abae88b5ead2e51690821d88300395.png)

If you specify "rangos.buckets.from" directly, it is undefined.

But once you `calculate` "rangos.buckets" field before transform, it could be defined.

> **[Editor/IDE for Vega and Vega-Lite](https://vega.github.io/editor/#/url/vega-lite/N4IgJAzgxgFgpgWwIYgFwhgF0wBwqgegIDc4BzJAOjIEtMYBXAI0poHsDp5kTykBaADZ04JAKyUAVhDYA7EABoQAEySYUqUMSSCGcCGgDaoANZwAnmgDMARgAsABidLlbKAH0obBrMxoxAJwA7ABsQTYOAExKAE5IsmRsBpogTAxQZpjJxiBmlug2lA78kQFFiiAAZjFsCGg2SphsaKUubp7evvUOdgC+CqYWaCBWDkX8VnblStW11g6NzaiTbR5ePn6oIQAcNv2D+SNTxQBUFbN1y3arHRv1IQ6T270Aur37uUOokSEPzirtdZdVCBUJ2MSRa4gOIJJJoUBpDJwLJGA7DQrFUrTKo1S4NEBNFoBG5AzZiBwBD55YajcaTbEXeaLaxQ1xrTqbKyRBx7AafQ7007nXEskkc+qRXbbEJ9N6vfoEmEQSpsGKXHJQHRQBiCNRwYaqTAMBCUGGJCCURGZAxKJDJEAoD6VXXYODyVCGB0gN4vJTIGImYY4Ng0LpKN1eZShsjwkAAD1jlRocEEymGVEZCvyoCTKbT6CohPevSAA)**
>
> The Vega editor is a web application for authoring and testing Vega and Vega-Lite visualizations. It includes a number of example specifications that showcase both the visual encodings and interaction techniques.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/0/e/0e332d0c0b0a92b1954706a554ec1b8234ad5a40.png)

I'm not sure about the reason of this behavior..

---

<div class="post-metadata">

**Author:** ![InesCM](https://avatars.discourse-cdn.com/v4/letter/i/9d8465/32.png) [@InesCM](https://discuss.elastic.co/u/InesCM)\
**Post date:** [July 14, 2022, 7:40am UTC](https://discuss.elastic.co/t/how-to-access-double-nested-data-in-kibana-vega/308806/6 "2022-07-14T07:40:07Z")

</div>

Hi!  
Thanks so much, it worked! The calculate+flatten allows me to access all the nested values. Now it's just a matter of playing with the fields until I plot something meaningful.

Cheers!  
Inés

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 11, 2022, 7:40am UTC](https://discuss.elastic.co/t/how-to-access-double-nested-data-in-kibana-vega/308806/7 "2022-08-11T07:40:39Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
