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


CONCATandSTRING_AGGbits. Looks like both are outputting a string."is present in your JSON then it should be escaped (that's what BQ is doing by adding` otherwise you won't have a valid JSON). You can test with simpler values and get the same result:SELECT to_json_string(['"5"', '"6"'])`. If you want a valid json then you probably shouldn't remove those from your output.