简体   繁体   English

在 Snowflake 中使用 SQL 获取 JSON 数组的索引

[英]Getting the index of the JSON array using SQL in Snowflake

I'm flattening a JSON data in snowflake using the Lateral Flatten.我正在使用横​​向展平将雪花中的 JSON 数据展平。

I have the JSON data as follows:我有如下的 JSON 数据:

{
   "Fruits": [
    {
      "Apple_Type" : Type_A,
      "Banana_Type": Type_B
    },
    {
      "Apple_Type" : Type_A2,
      "Banana_Type": Type_B3
    }
  ]
}

I used the following query to get the flattened data我使用以下查询来获取扁平化数据

SELECT  v.value:Apple_Type,
        v.value:Banana_Type
FROM Table1, LATERAL FLATTEN(input => Fruits) v

My Result:我的结果:

--------------------------------
| Apple_Type    |  Banana_Type |
--------------------------------
| Type_A        |    Type_B   |
| Type_A2       |    Type_B3   |
--------------------------------

How do I get the index of the data.如何获取数据的索引。 I want the table as follows我想要表格如下

----------------------------------------------
| Apple_Type    |  Banana_Type |    Index    |
----------------------------------------------
| Type_A        |    Type_B   |      0      | -> Because Apple_Type is from index 0 in the Fruit Array
| Type_A2       |    Type_B3   |      1      | -> Because Banana_Type is from index 1 in the Fruit Array
---------------------------------------------- 

Using INDEX :使用INDEX

INDEX指数

The index of the element, if it is an array;元素的索引,如果是数组; otherwise NULL.否则为 NULL。

SELECT  v.value:Apple_Type,
        v.value:Banana_Type,
        v.index
FROM Table1, LATERAL FLATTEN(input => Fruits) v

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

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