简体   繁体   English

在redshift中提取具有相同键的键值对

[英]Extract key value pairs with the same key in redshift

I have a requirement where I have a json array on each cell of the column.我有一个要求,在列的每个单元格上都有一个 json 数组。 I am trying to find a way to extract each instance of the same key value pair.我试图找到一种方法来提取相同键值对的每个实例。 For example, I would like to extract each occurrence of 'product_id' from the following array:例如,我想从以下数组中提取每次出现的“product_id”:

[{"id": 11993176146155, "fulfillable_quantity": 0, "product_id": 7538905317611, "properties": [], "quantity": 1, "requires_shipping": true, "taxable": false, "total_discount": "0.00", "total_discount_set": {"shop_money": {"amount": "0.00", "currency_code": "PKR"}, "presentment_money": {"amount": "0.00", "currency_code": "PKR"}}}, {"id": 11993176178923, "price_set": {"shop_money": {"amount": "450.00", "currency_code": "PKR"}, "presentment_money": {"amount": "450.00", "currency_code": "PKR"}}, "product_exists": true, "product_id": 7018040164543}] [{“id”:11993176146155,“fulfillable_quantity”:0,“product_id”:7538905317611,“properties”:[],“quantity”:1,“requires_shipping”:true,“taxable”:false,“total_discount”:“ 0.00", "total_discount_set": {"shop_money": {"amount": "0.00", "currency_code": "PKR"}, "presentment_money": {"amount": "0.00", "currency_code": "PKR" }}}, {"id": 11993176178923, "price_set": {"shop_money": {"amount": "450.00", "currency_code": "PKR"}, "presentment_money": {"amount": "450.00" , "currency_code": "PKR"}}, "product_exists": true, "product_id": 7018040164543}]

I want to create a new column called 'Product_ID' and store the extracted values in this column.我想创建一个名为“Product_ID”的新列并将提取的值存储在此列中。 My final result should look like this:我的最终结果应该是这样的:

在此处输入图像描述

Is there a way in redshift to iterate over the entire array for each row and extract the desired values (in this case it is product IDs)? redshift 中是否有一种方法可以为每一行遍历整个数组并提取所需的值(在本例中为产品 ID)? I have looked at some functions on redshift but unfortunately those didn't help.我已经查看了 redshift 上的一些功能,但不幸的是那些没有帮助。 Would be great if someone could provide a solution.如果有人可以提供解决方案,那就太好了。

You need to unnest the array and then select the keys you want.您需要取消嵌套数组,然后 select 您想要的键。

There is a great tutorial for unnesting arrays in Redshift here: https://blog.getdbt.com/how-to-unnest-arrays-in-redshift/这里有一个很棒的教程,用于在 Redshift 中取消嵌套 arrays: https://blog.getdbt.com/how-to-unnest-arrays-in-redshift/

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

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