我有一个带有json数组的json文件list_values.json,该数组带有一个"Origine"字段:
[
{
"origine": "reason1",
"identifiant": "1234_AAA"
},
{
"origine": "reason3",
"identifiant": "5678_BBB"
}
]
我有mapping.json个文件,其中包含一系列预定义的字段:
{
"REASON_0": "reason0",
"REASON_1": "reason1",
"REASON_2": "reason2",
"REASON_3": "reason3",
"REASON_4": "reason4"
}
我使用JQ编写了一个bash脚本,代码如下:
jq '.[] |= . + {"type": (.identifiant | split("_") | .[1] )}' list_values.json > add_fields.json
个
我的档案add_fields.json:
[
{
"origine": "reason1",
"identifiant": "1234_AAA",
"type": "AAA"
},
{
"origine": "reason3",
"identifiant": "5678_BBB",
"type": "BBB"
}
]
我想添加一个等于关键字mapping.json的"Reason"字段,其值与"Origine"字段list_values.json匹配.如下所示:
[
{
"origine": "reason1",
"identifiant": "1234_AAA",
"type": "AAA",
"reason": "REASON_1"
},
{
"origine": "reason3",
"identifiant": "5678_BBB",
"type": "BBB",
"reason": "REASON_3"
}
]
我能用JQ做什么呢?
我指定值mapping.json作为键是唯一的.