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

add find and replace to json extract #92

Merged
merged 9 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from 8 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
## Bug Fixes
[PR #91](https://github.com/fivetran/dbt_fivetran_utils/pull/91) updates dispatch from `{{ dbt_utils.<macro> }}` to `{{ dbt.<macro> }}` for additional [cross-db macros](https://docs.google.com/spreadsheets/d/1xF_YwJ4adsnjFkUbUm8-EnEL1r_C-9_OI_pP4m4FlJU/edit#gid=1062533692) missed in the `fivetran_utils.union_relations()` macro.

## Updates
[PR #92](https://github.com/fivetran/dbt_fivetran_utils/pull/92) updates the `pivot_json_extract` macro to include additional formats of fields to be pivoted out into columns. Specifically, allowing for fields with `.` to be replaced with `_`, for metadata variables to accept dictionaries in addition to strings, and for aliasing of fields.

# dbt_fivetran_utils v0.4.0

## Bug Fixes
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ This macro builds off of the `json_extract` macro in order to extract a list of
```
**Args:**
* `string` (required): Name of the field which contains the json object.
* `list_of_properties` (required): List of the fields that you want to extract from the json object and pivot out into columns. Any spaces will be replaced by underscores in column names.
* `list_of_properties` (required): List of the fields that you want to extract from the json object and pivot out into columns.

----
### string_agg ([source](macros/string_agg.sql))
Expand Down
4 changes: 4 additions & 0 deletions macros/pivot_json_extract.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
{% macro pivot_json_extract(string, list_of_properties) %}

{%- for property in list_of_properties -%}
{%- if property is mapping -%}
replace( {{ fivetran_utils.json_extract(string, property.name) }}, '"', '') as {{ property.alias if property.alias else property.name | replace(' ', '_') | replace('.', '_') | lower }}

{%- else -%}
replace( {{ fivetran_utils.json_extract(string, property) }}, '"', '') as {{ property | replace(' ', '_') | lower }}

{%- endif -%}
{%- if not loop.last -%},{%- endif %}
{% endfor -%}

Expand Down