0

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!

1 Answer 1

1

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"
]
Sign up to request clarification or add additional context in comments.

2 Comments

Quick question though - not directly related. Do you know why my arg are not correctly interpreted? jq --arg EnvType ${env} --arg KMSID ${kmsid} '. + ["EnvType=[$EnvType]", "KMSID=[$KMSID]"]' config.json
@Mornor Unless you put them into string interpolation parens (like "EnvType=[\($EnvType)]"), variable names in strings are not parsed and expanded.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.