String byte to String - Big query

限于喜欢 提交于 2019-12-13 03:36:21

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!