如何使用 jq 将 json 部分添加到现有文件
How to add section of json to the existing file using jq
我正在使用 jq
来解析 JSON 文件。我有 JSON 文件的某些部分,内容如下
[
{
"PropagateAtLaunch": true,
"Value": {
"Ref": "TNAM"
},
"Key": "Name"
},
{
"PropagateAtLaunch": true,
"Value": {
"Ref": "TAPP"
},
"Key": "application"
},
{
"PropagateAtLaunch": true,
"Value": {
"Ref": "TENV"
},
"Key": "environment"
},
{
"PropagateAtLaunch": true,
"Value": {
"Ref": "TSHA"
},
"Key": "shared"
},
{
"PropagateAtLaunch": true,
"Value": {
"Ref": "TTER"
},
"Key": "tier"
},
{
"PropagateAtLaunch": true,
"Value": {
"Ref": "CostCenter"
},
"Key": "cost-center"
}
]
在此我想添加另一个部分,例如:
{
"Value": {
"Ref": "TEAM"
},
"PropagateAtLaunch": true,
"Key": "TEAM"
}
如何添加这个新部分?
这是我用来提取第一部分的查询:
$ cat ABC.json | jq '.Resources.ASGRP.Properties.Tags'
试试下面的“jq -s add ABC.json add.json
”;
user@host:/tmp$ cat add.json
[
{
"Value": {
"Ref": "TEAM"
},
"PropagateAtLaunch": true,
"Key": "TEAM"
}
]
user@host:/tmp$ jq -s add ABC.json add.json > ABCLAST.json
user@host:/tmp$ cat ABCLAST.json
[
{
"PropagateAtLaunch": true,
"Value": {
"Ref": "TNAM"
},
"Key": "Name"
},
{
"PropagateAtLaunch": true,
"Value": {
"Ref": "TAPP"
},
"Key": "application"
},
{
"PropagateAtLaunch": true,
"Value": {
"Ref": "TENV"
},
"Key": "environment"
},
{
"PropagateAtLaunch": true,
"Value": {
"Ref": "TSHA"
},
"Key": "shared"
},
{
"PropagateAtLaunch": true,
"Value": {
"Ref": "TTER"
},
"Key": "tier"
},
{
"PropagateAtLaunch": true,
"Value": {
"Ref": "CostCenter"
},
"Key": "cost-center"
},
{
"PropagateAtLaunch": true,
"Value": {
"Ref": "TEAM"
},
"Key": "TEAM"
}
]
您可以使用 --argjson
将对象作为 jq 变量传递,然后使用 +=
将对象添加到数组:
jq --argjson obj '{"sample": "object"}' '.Resources.ASGRP.Properties.Tags += [$obj]'
如果您不想要原始对象,请使用 +
而不是 +=
。
我正在使用 jq
来解析 JSON 文件。我有 JSON 文件的某些部分,内容如下
[
{
"PropagateAtLaunch": true,
"Value": {
"Ref": "TNAM"
},
"Key": "Name"
},
{
"PropagateAtLaunch": true,
"Value": {
"Ref": "TAPP"
},
"Key": "application"
},
{
"PropagateAtLaunch": true,
"Value": {
"Ref": "TENV"
},
"Key": "environment"
},
{
"PropagateAtLaunch": true,
"Value": {
"Ref": "TSHA"
},
"Key": "shared"
},
{
"PropagateAtLaunch": true,
"Value": {
"Ref": "TTER"
},
"Key": "tier"
},
{
"PropagateAtLaunch": true,
"Value": {
"Ref": "CostCenter"
},
"Key": "cost-center"
}
]
在此我想添加另一个部分,例如:
{
"Value": {
"Ref": "TEAM"
},
"PropagateAtLaunch": true,
"Key": "TEAM"
}
如何添加这个新部分?
这是我用来提取第一部分的查询:
$ cat ABC.json | jq '.Resources.ASGRP.Properties.Tags'
试试下面的“jq -s add ABC.json add.json
”;
user@host:/tmp$ cat add.json
[
{
"Value": {
"Ref": "TEAM"
},
"PropagateAtLaunch": true,
"Key": "TEAM"
}
]
user@host:/tmp$ jq -s add ABC.json add.json > ABCLAST.json
user@host:/tmp$ cat ABCLAST.json
[
{
"PropagateAtLaunch": true,
"Value": {
"Ref": "TNAM"
},
"Key": "Name"
},
{
"PropagateAtLaunch": true,
"Value": {
"Ref": "TAPP"
},
"Key": "application"
},
{
"PropagateAtLaunch": true,
"Value": {
"Ref": "TENV"
},
"Key": "environment"
},
{
"PropagateAtLaunch": true,
"Value": {
"Ref": "TSHA"
},
"Key": "shared"
},
{
"PropagateAtLaunch": true,
"Value": {
"Ref": "TTER"
},
"Key": "tier"
},
{
"PropagateAtLaunch": true,
"Value": {
"Ref": "CostCenter"
},
"Key": "cost-center"
},
{
"PropagateAtLaunch": true,
"Value": {
"Ref": "TEAM"
},
"Key": "TEAM"
}
]
您可以使用 --argjson
将对象作为 jq 变量传递,然后使用 +=
将对象添加到数组:
jq --argjson obj '{"sample": "object"}' '.Resources.ASGRP.Properties.Tags += [$obj]'
如果您不想要原始对象,请使用 +
而不是 +=
。