Mornor
Mornor

Reputation: 3803

Add new elements to JSON array

I have a config file that is like

[
  "ECSClusterName=cluster",
  "VPCID=vpc-xxxx",
  "ALBName=ALB"
]

And with jq (or something else bash-native), I'd like to add 2 values - EnvType and KMSID - (doesn't matter where in the config file) so that the end result would look like

[
  "EnvType=dev",
  "KMSID=xxxxx-yyyyyy-ffffff",
  "ECSClusterName=cluster",
  "VPCID=vpc-xxxx",
  "ALBName=ALB"
]

The closest I have been for one value is

cat config.json | jq '.[-1] += ", test=test"'

But that outputs

[
  "ECSClusterName=cluster",
  "VPCID=vpc-xxxx",
  "ALBName=ALB, test=test"
] 

Any help greatly appreciated!

Upvotes: 0

Views: 102

Answers (1)

oguz ismail
oguz ismail

Reputation: 50805

Put new key=value pairs into an array, and add that array to the original one.

$ jq '. + ["EnvType=dev", "KMSID=xxxxx-yyyyyy-ffffff"]' config.json
[
  "ECSClusterName=cluster",
  "VPCID=vpc-xxxx",
  "ALBName=ALB",
  "EnvType=dev",
  "KMSID=xxxxx-yyyyyy-ffffff"
]

Upvotes: 1

Related Questions