问题
I have the below query and it produces data with \"
added. Not sure if it is string byte causing this problem because string_agg
produces string byte as output.
#standardSQL
SELECT
visitid,
fullVisitorId,
hits.hitNumber,
TO_JSON_STRING(ARRAY(
SELECT
AS STRUCT productSKU,
ARRAY(SELECT STRING_AGG(CONCAT('{"',CAST(index AS STRING), '":', '"', IFNULL(value,''), '"', '}'), ',') FROM UNNEST(customDimensions)) as productCustDimension
FROM
UNNEST(hits.product) p)) AS product
FROM
table_a
LEFT JOIN
UNNEST(hits) AS hits
LIMIT 10
Below is the output it is producing. In the product column, the productCustDimension
value is what i am taking about, with addition "\
characters added.
I found a way to get rid of extra characters I mentioned using replace function, but it kind of becomes too dirty. I am looking if there is a better way of doing it. I want the product column as below
回答1:
The problem is with the " character which is escaped in TO_JSON_STRING. Try running the query like so:
...
ARRAY(SELECT STRING_AGG(CONCAT('{',CAST(index AS STRING), ':', '', IFNULL(value,''), '', '}'), ',') FROM UNNEST(customDimensions)) as productCustDimension
...
来源:https://stackoverflow.com/questions/48508317/string-byte-to-string-big-query