![](/img/trans.png)
[英]Update one JSON file values with values from another JSON using JQ (on all levels)
[英]Replacing values in JSON using values from another file with jq
我有一个包含城市名称的 json 文件,我想用另一个文件中的特定城市代码替换它们。 data.json
大致是:
{
"Customer": {
"CustomerName": "Customer1",
"City": "Cityname1"
}
}
{
"RelevantObject": false
}
{
"Customer": {
"CustomerName": "Customer2",
"City": "Cityname2"
}
}
# {...
代码列表可以是最容易提供给 jq 的任何内容,我一直在尝试使用codes.json
:
{
"Cityname1": "Code1",
"Cityname2": "Code2"
}
但就像我说的,任何格式都可以。 希望的结果:
{
"Customer": {
"CustomerName": "Customer1",
"City": "Code1"
}
}
{
"RelevantObject": false
}
{
"Customer": {
"CustomerName": "Customer2",
"City": "Code2"
}
}
我一直在尝试使用jq --argfile codes codes.json
读取文件,但我很难参考jq
的$codes
: .Customer.City=$codes.?
?
在这里得到一个好的答案的关键是:
.Customer.City |= $codes[.]
jq --argfile codes codes.json 'select(.Customer?)|{(.Customer.City):$codes[.Customer.City]}' data.json | jq -n '[inputs] | add'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.