简体   繁体   中英

How to create a subset from a variant column in snowflake?

For example, suppose my variant column "xyz" consists of data like this:

{
  "post_new_visits": "Repeat",
  "post_new1_week": "Thursday",
  "post_new2_appt": "Weekday",
  "post_new3_site": "12:50AM",
  "post_new4_channel": "5.0",
  "pre_new2_appt": "Weekday",
  "pre_new3_site": "12:50AM",
  "pre_new4_channel": "5.0"
}

I want a new variant column from above variant column, which should only have "post*" key values, something like below,output should be like this.

{
  "post_new_visits": "Repeat",
  "post_new1_week": "Thursday",
  "post_new2_appt": "Weekday",
  "post_new3_site": "12:50AM",
  "post_new4_channel": "5.0",
}

Is there any way i can acheive this?

Maybe you can flatten and re-construct the JSON. For example:

create table tmp ( v variant )
as 
select
    parse_json(
        '{  "post_new_visits": "Repeat",
          "post_new1_week": "Thursday",
          "post_new2_appt": "Weekday",
          "post_new3_site": "12:50AM",
          "post_new4_channel": "5.0",
          "pre_new2_appt": "Weekday",
          "pre_new3_site": "12:50AM",
          "pre_new4_channel": "5.0"}'
    )
union all                              
select
    parse_json(
        '{  "post_new_visits": "New",
          "post_new1_week": "Friday",
          "post_new2_appt": "Weekday",
          "post_new3_site": "13:50AM",
          "post_new4_channel": "4.0",
          "pre_new2_appt": "Weekday",
          "pre_new3_site": "14:50AM",
          "pre_new4_channel": "2.0"}'
    );                                      




select
    OBJECT_AGG(v2.key, v2.value)
from
    tmp,
    lateral flatten(input => v) v2
where
    v2.key like 'post%'
group by
    v2.seq;

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