如何使用JSONATA将JSON层级结构拆分为独立表格记录
使用JSONATA拆分层级JSON为独立记录
要实现将你提供的层级JSON拆分为每条带日期字段的独立记录,用以下JSONATA表达式即可:
$flatten($each($$, function($moduleList, $date) { $moduleList.{ "date": $date } }))
表达式说明
$$指代整个输入的JSON对象$each($$, function($moduleList, $date) { ... }):遍历顶层对象的键值对,$date就是日期字符串(比如示例里的"2022-09-22"),$moduleList是对应日期下的模块数组$moduleList.{ "date": $date }:给数组里的每个模块对象新增date字段,值为当前遍历到的日期$flatten(...):把遍历后得到的数组的数组(每个日期对应一个数组)展开成一维数组,得到最终的扁平记录结构
验证效果
输入JSON:
{"2022-09-22": [{"name": "modules/dynatrace","count": 60},{"name": "modules/dynatrace/monitors/http-monitors/basic","count": 4},{"name": "modules/splunk/hec-token","count": 14},{"name": "modules/aws/lambda/logs_streaming_splunk","count": 29}]}
经过表达式转换后,输出结果:
[{"date": "2022-09-22","name": "modules/dynatrace","count": 60},{"date": "2022-09-22","name": "modules/dynatrace/monitors/http-monitors/basic","count": 4},{"date": "2022-09-22","name": "modules/splunk/hec-token","count": 14},{"date": "2022-09-22","name": "modules/aws/lambda/logs_streaming_splunk","count": 29}]
内容的提问来源于stack exchange,提问作者Ashish Sharma
相关产品推荐
相关产品推荐

