diff --git a/CHANGELOG.md b/CHANGELOG.md index 6df06be..d49a9f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,10 @@ # dbt_fivetran_utils v0.4.1 ## Bug Fixes -[PR #91](https://github.com/fivetran/dbt_fivetran_utils/pull/91) updates dispatch from `{{ dbt_utils. }}` to `{{ dbt. }}` 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 dispatch from `{{ dbt_utils. }}` to `{{ dbt. }}` 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. ([PR #91](https://github.com/fivetran/dbt_fivetran_utils/pull/91)) + +## Updates +- Updates the `pivot_json_extract` macro to include additional formats of fields using a new `name` and `alias` argument 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. ([PR #92](https://github.com/fivetran/dbt_fivetran_utils/pull/92)) # dbt_fivetran_utils v0.4.0 diff --git a/README.md b/README.md index 0dabc0f..b2900ed 100644 --- a/README.md +++ b/README.md @@ -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)) diff --git a/macros/pivot_json_extract.sql b/macros/pivot_json_extract.sql index 025b683..8891c2f 100644 --- a/macros/pivot_json_extract.sql +++ b/macros/pivot_json_extract.sql @@ -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 -%}