json 如何解析字典的字符串列表?

s3fp2yjn  于 2023-05-19  发布在  其他
关注(0)|答案(1)|浏览(360)

如何在BigQuery中解析字符串list of dictionary

  • 样本:'[{"Type":value, "ID":value, "Amount":value} ,{"Type":value, "ID":value, "Amount":value}]'

可能看起来类似于12,但天真地使用ARRAYSPLITJSON_QUERY是不起作用的,因为字典有逗号。
c.f.而不是BigQuery解决方案(Python:3,Java:4

试过

WITH
logs AS (
    SELECT '[{"Type":value, "ID":value, "Amount":value} ,{"Type":value, "ID":value, "Amount":value}]' as inventory,
)
SELECT
    inventory,  -- org
    ARRAY(inventory)  -- try 1
    SPLIT(SUBSTR(inventory, 2, length(inventory) - 2), ',') as inventory,  -- try 2
    JSON_QUERY(SPLIT(SUBSTR(inventory, 2, length(inventory) - 2), ','),'$')  -- try 3
    ...
FROM logs
;
oymdgrw7

oymdgrw71#

对于您的要求,您可以考虑以下查询:

WITH
  logs AS (
  SELECT
    '[{"Type": "shirt", "ID": 123, "Amount": 1}, {"Type": "pants", "ID": 456, "Amount": 2}]' AS inventory )
    
SELECT
  JSON_EXTRACT_SCALAR(unnested, '$.Type') AS Type,
  JSON_EXTRACT_SCALAR(unnested, '$.ID') AS Id,
  JSON_EXTRACT_SCALAR(unnested, '$.Amount') AS Amount,
FROM
  logs,
  UNNEST(JSON_EXTRACT_ARRAY(inventory)) AS unnested

查询结果:

相关问题