Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle extracting root using json_value #18

Merged
merged 4 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions macros/json_value.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@

{%- macro snowflake__json_value(column, path, array, dtype, skip_parse) -%}
{# Remove the leading $. from the path #}
{% set path = modules.re.sub('\$\.', '', path) %}
{%- set path = modules.re.sub('\$\.?', '', path) -%}
{# Replace dots with colons in the path #}
{% set path = modules.re.sub('\.', ':', path) %}
{{column}}:{{path}}
{%- set path = modules.re.sub('\.', ':', path) -%}
{# If there is a path, prefix with a colon #}
{%- if path != '' -%}
{%- set path = ':' + path -%}
{%- endif -%}
{{column}}{{path}}
{%- endmacro -%}
10 changes: 10 additions & 0 deletions tests/json_value_child.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
with x as (
{%- if target.type == 'snowflake' -%}
select PARSE_JSON('{"good_key":"good_value"}') as col
{%- else -%}
select '{"good_key":"good_value"}' as col
{%- endif -%}
)
select *
from x
where {{ json_value("col", "$.good_key") }} != 'good_value'
10 changes: 10 additions & 0 deletions tests/json_value_root.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
with x as (
{%- if target.type == 'snowflake' -%}
select PARSE_JSON('{"good_key":"good_value"}') as col
{%- else -%}
select '{"good_key":"good_value"}' as col
{%- endif -%}
)
select *
from x
where {{ json_value("col", "$") }} != '{"good_key":"good_value"}'