简体   繁体   English

如何在雪花中展平json? sql

[英]How to flatten a json in snowflake? sql

I have a table "table_1" with one column called "Value" and it only has one entry.我有一个表“table_1”,其中有一列名为“Value”,它只有一个条目。 The entry in the column is a json that looks like列中的条目是一个 json,看起来像

{
  "c1": "A",
  "c10": "B",
  "c100": "C",
  "c101": "D",
  "c102": "E",
  "c103": "F",
  "c104": "G",
.......
}

I would like to just separate this json into two columns, where one column contains the keys (c1, c10 etc), and the second columns contains the associated values for that key (A, B etc).我只想将此 json 分成两列,其中一列包含键(c1、c10 等),第二列包含该键的关联值(A、B 等)。 Is there a way I can do this?有没有办法我可以做到这一点? There are about 125 keys in my json我的 json 中有大约 125 个键

It is possible to achieve it using FLATTEN function:可以使用 FLATTEN 函数来实现它:

CREATE OR REPLACE TABLE tab
AS
SELECT PARSE_JSON('{
  "c1": "A",
  "c10": "B",
  "c100": "C",
  "c101": "D",
  "c102": "E",
  "c103": "F",
  "c104": "G",
}') AS col;

SELECT KEY, VALUE::TEXT AS value
FROM tab
,TABLE(FLATTEN (INPUT => tab.COL));

Output:输出:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM