You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:如何在Snowflake中将列内嵌套JSON格式的非结构化数据拆分出accountNumber与orgUrns独立列

Solution to Parse DynamoDB-Style JSON in Snowflake

Hey there! I’ve run into this exact DynamoDB-style JSON format in Snowflake before—those nested S (string) and L (list) wrappers can be tricky at first, but once you map out the structure, it’s straightforward to extract the values you need.

Let’s break down your data structure first: your column holds a JSON array with a single object, which contains an M map. Inside that map, accountNumber is a string wrapped in S, and orgUrns is a list wrapped in L.

Basic Extraction (Single Element Array)

Assuming your column is named raw_json_data and your table is your_table, here’s a SQL query to pull out accountNumber and orgUrns into separate columns:

SELECT
  -- Extract the account number string
  PARSE_JSON(raw_json_data)[0]:M:accountNumber:S AS account_number,
  -- Extract the orgUrns as an array
  PARSE_JSON(raw_json_data)[0]:M:orgUrns:L AS org_urns_array,
  -- Optional: Convert the array to a comma-separated string for easier reading
  ARRAY_TO_STRING(PARSE_JSON(raw_json_data)[0]:M:orgUrns:L, ', ') AS org_urns_string
FROM your_table;

Let me explain each part:

  • PARSE_JSON(raw_json_data) converts your string column into a Snowflake JSON object.
  • [0] grabs the first (and only, in your example) element from the outer array.
  • :M accesses the nested map inside that element.
  • :accountNumber:S drills down to the string value of accountNumber.
  • :orgUrns:L pulls out the list of org URNs as a Snowflake array.

Handling Multiple Elements in the Array

If your JSON array ever has more than one M object, use LATERAL FLATTEN to expand the array into individual rows:

SELECT
  flattened.value:M:accountNumber:S AS account_number,
  flattened.value:M:orgUrns:L AS org_urns_array,
  ARRAY_TO_STRING(flattened.value:M:orgUrns:L, ', ') AS org_urns_string
FROM your_table,
LATERAL FLATTEN(input => PARSE_JSON(raw_json_data)) AS flattened;

Handling NULL Values

To avoid NULLs in your results if some entries are missing accountNumber or orgUrns, wrap the extractions in IFNULL:

SELECT
  IFNULL(PARSE_JSON(raw_json_data)[0]:M:accountNumber:S, 'No Account Number') AS account_number,
  IFNULL(PARSE_JSON(raw_json_data)[0]:M:orgUrns:L, ARRAY_CONSTRUCT()) AS org_urns_array
FROM your_table;

This should cover most scenarios—let me know if you run into edge cases with your specific data!

内容的提问来源于stack exchange,提问作者Nadeem

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 20:12:45