使用Azure Data Factory转换异构JSON时currentValue空值问题求解
解决方案
问题根因
currentValue返回空值是因为ADF数据流默认会根据首条数据推断JSON字段类型,首条currentValue为数组,后续字符串、对象类型不匹配推断类型时就会被解析为空。
方案1:直接转换为目标Schema
步骤1:配置源数据集避免类型推断错误
- 在源JSON数据集的设置中,将
文档格式设置为数组 of 对象 - 导入Schema后,手动将
doc数组下的currentValue字段类型修改为dynamic,不指定固定类型
步骤2:展开doc数组
添加Flatten转换,选择展开doc数组,输出保留以下字段:
key.iddoc.propertydoc.currentValue
步骤3:提取目标属性名
添加Derived Column转换,新增propertyName字段,表达式为:
split(doc.property, '.')[2]
该表达式会将org.numberOfEmployees这类属性名截取为numberOfEmployees,匹配目标Schema的字段名。
步骤4:按ID分组聚合生成完整记录
添加Aggregate转换:
- 分组键设置为
key.id - 新增以下聚合字段,对应目标Schema:
id:toInteger(key.id)startDate:max(iif(propertyName == 'startDate', currentValue, toString(null())))numberOfEmployees:max(iif(propertyName == 'numberOfEmployees', currentValue, array(null())))employeeCount:toInteger(max(iif(propertyName == 'employeeCount', currentValue, toString(null()))))headOffice:max(iif(propertyName == 'headOffice', currentValue, toDynamic('{}')))
聚合后每个ID对应一条完整的目标结构记录,可直接写入目标NoSQL集合。
方案2:将currentValue序列化(stringify)
如果不需要直接转目标Schema,可在Derived Column转换中直接对dynamic类型的currentValue执行序列化,表达式为:
toString(currentValue)
该表达式支持将任意类型(数组、对象、数值、字符串)的currentValue序列化为标准JSON字符串,不会返回空值。
注意事项
- 若同一个ID下存在同一属性的多条更新记录,可将聚合函数
max替换为first/last,按业务需求取最早/最新的更新值 - 调试时可开启数据流的预览模式,确认
currentValue字段不再为空后再执行全量运行
内容的提问来源于stack exchange,提问作者Biju Thomas
相关产品推荐
相关产品推荐

