2

I have a JSON field my table which looks like this. There could be any number of menu categories and any number of items

{
"menu": {
    "salad": [
        {
            "item": "russian salad"
        },
        {
            "item": "french salad"
        }
    ],
    "soups": [
        {
            "item": "french soup"
        },
        {
            "item": "english soup"
        },
        {
            "item": "english soup"
        }
    ]
}

There could be any number of menu category (salad, soup) plus any number of items within each category

salad | 2
soups | 3

I've looked at my own previous question, but unsure how I can modify to accommodate for any number of categories and items?

Tom
  • 33,626
  • 31
  • 85
  • 109

1 Answers1

3

Consider below approach

select key, array_length(split(values)) items
from `project.dataset.table`,
unnest(`bqutil.fn.json_extract_keys`(json_extract(col, '$.menu'))) key with offset 
join unnest(`bqutil.fn.json_extract_values`(json_extract(col, '$.menu'))) values with offset 
using(offset)          

if applied to sample data in your question - output is

enter image description here

Mikhail Berlyant
  • 165,386
  • 8
  • 154
  • 230
  • 1
    Thanks once again. Just to note for anyone else struggling with bgutil, it's not currently (Jan 2021) available outside the US as a public dataset, it needs to be deployed, https://github.com/GoogleCloudPlatform/bigquery-utils/issues/252 – Tom Jan 06 '22 at 10:26