From 248346d94bf07d703faa71eaa5eab6cf24773434 Mon Sep 17 00:00:00 2001 From: Jacob Mulligan Date: Sat, 29 Apr 2023 11:44:00 +0200 Subject: [PATCH] Patch to handle JSON that's not inside an array. --- target_bigquery/core.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/target_bigquery/core.py b/target_bigquery/core.py index b44c498..369d2a4 100644 --- a/target_bigquery/core.py +++ b/target_bigquery/core.py @@ -862,18 +862,18 @@ def _wrap_json_array( field, path="$", depth=depth, base=f"{field.name}__rows" ) v = _v.as_sql().rstrip(", \n") + return (" " * depth * 2) + indent( + + + # This is a patch to get around the fact that JSON_QUERY_ARRAY doesn't work with + # fields that contain a single JSON blob instead of a an array of JSON blobs. dedent( f""" - ARRAY( - SELECT {v} - FROM UNNEST( - JSON_QUERY_ARRAY({base}, '{path}.{field.name}') - ) AS {field.name}__rows - WHERE {_v.projection} IS NOT NULL - """ + JSON_QUERY({base}, '{path}.{field.name}') + """ + (" " * depth * 2) - + f") AS {field.name},\n" + + f" AS {field.name},\n" ).lstrip(), " " * depth * 2, )