如何在Azure Synapse Data Flow中将键值对JSON展平为列?
Azure Synapse Data Flow 嵌套JSON键值对转表格解决方案
你需要把嵌套的scoreIndeces键值对JSON结构转换成包含scoreIndecesKey和scoreIndecesValue的表格形式,可通过以下步骤实现:
原始JSON结构
"scoreIndeces": { "66": 0.2, "67": 0.3, "68": 0.4, "70": 0.5, "71": 0.6, "73": 0.7, "77": 0.8, "78": 0.9, "80": 1, "81": 1.1, "82": 1.2, "83": 1.3, "84": 1.4, "85": 1.5, "86": 1.6, "87": 1.7, "88": 1.8, "89": 1.9 }
目标表格格式
| scoreIndecesKey | scoreIndecesValue |
|---|---|
| 66 | 0.2 |
| 67 | 0.3 |
具体转换步骤
步骤1:用派生列将对象转成键值对数组
添加派生列组件,新建列scoreArray,输入表达式:mapObjectKeysValues(scoreIndeces)这个函数会把
scoreIndeces对象转换成数组,每个元素是{"key":"66", "value":0.2}这样的结构。步骤2:扁平化数组生成多行
添加扁平化(Flatten)组件,在"要扁平化的数组"中选择scoreArray,模式选"生成行",这样数组里的每个键值对对象都会变成单独一行数据。步骤3:提取键和值生成目标列
再添加一个派生列组件,分别创建两个新列:scoreIndecesKey:表达式填toString(scoreArray.key)(确保键为字符串类型)scoreIndecesValue:表达式填toDouble(scoreArray.value)(确保值为数值类型,可根据实际需求调整类型)
步骤4:清理冗余列(可选)
用选择组件删除不需要的原始列(比如scoreIndeces和scoreArray),只保留scoreIndecesKey和scoreIndecesValue两列。
完成以上步骤后,就能得到你需要的表格形式数据。
内容的提问来源于stack exchange,提问作者Balaji Dev
相关产品推荐
相关产品推荐

