简体   繁体   中英

How to parse out a JSON variable in Snowflake using SQL

So I have a dataset and it has a column which has data like this:

ID     values
0001   [
       {
        prices: {
          ia: '20K+',
          ln: '3K-10K'
        },
        formats: [
          'n',
          'ia'
        ],
        id: 'c8f4f498-1cfeaf1-455a-a5ac-310191wefw959583',
        image_url: 'file.jpg',
        slug: 'test1'
      },
       {
        prices: {
          ia: '20K+',
          ln: '3K-10K'
        },
        formats: [
          'n',
          'ia'
        ],
        id: 'c8f4f4wfwe98-1ca1-455a-a5ac-3101919wfewf59583',
        image_url: 'file.jpg',
        slug: 'test3'
      }
    ]
0002   [
       {
        prices: {
          ia: '20K+',
          ln: '3K-10K'
        },
        formats: [
          'n',
          'ia'
        ],
        id: 'c8f4feeee498-1ca1-455a-a5ac-3101919fwewf59583',
        image_url: 'file.jpg',
        slug: 'test2'
      }

All I care about in this variable is the slug . But different ID's have different number of slugs. How could I get a new df from this data to show me every slug as well as the count.

So the result I want:

  ID              slugs  slug_count
0001  ['test1','test3']           2
0002          ['test2']           1

Flatten and group:

with data as (
select $1 id, parse_json($2) j
from values(1, '[{"slug":"a"}]'), (2, '[{"slug":"a1"},{"slug":"a2"}]')
)

select id, array_agg(x.value:slug) slugs, count(*) slug_count
from data, table(flatten(j)) x
group by id

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM