change198
change198

Reputation: 2055

Create a json file using jq and multiple variables

I have a json file like this:

{
  "name": "Job",
  "type": "xdb",
  "typeLogoUrl": "public/app/plugins/logo.svg",
  "access": "proxy",
  "url": "http://xdb:80",
  "password": {},
  "user": "xx",
  "database": "Job",
  "basicAuth": true,
  "basicAuthUser": "xx",
  "basicAuthPassword": {},
  "withCredentials": true,
  "isDefault": false,
  "jsonData": {},
  "secureJsonFields": null
}

Now all I want is to pass environment variables to key password and basicAuthPassword and to generate new json file as below:

{
  "name": "Job",
  "type": "xdb",
  "typeLogoUrl": "public/app/plugins/logo.svg",
  "access": "proxy",
  "url": "http://xdb:80",
  "password": "password" ,
  "user": "xx",
  "database": "Job",
  "basicAuth": true,
  "basicAuthUser": "xx",
  "basicAuthPassword": "password",
  "withCredentials": true,
  "isDefault": false,
  "jsonData": {},
  "secureJsonFields": null
}

I have tried like below to replace at least one key and it's giving me null.

/usr/local/bin/jq -n --arg "password" '.password = $arg' < input.json

Can anyone suggest me how to achieve this?

Upvotes: 0

Views: 1408

Answers (2)

peak
peak

Reputation: 116680

If the password is already an environment variable (which may not be such a good idea though), then to avoid exposing it on the command-line, you could use the env builtin, along the lines of:

.password = env.password

etc.

An alternative to consider would be putting the password into a temporary file, and then using the --argfile command-line option, for example. Or if your shell supports it, --argfile pw <(echo "\"$password\"")

Upvotes: 1

oliv
oliv

Reputation: 13239

Security wise, it's a bad idea to pass password in command line, but I hope you know what you're doing.

Assuming that, you need to modify your jq command like this:

jq --arg p "password" '.password = $p | .basicAuthPassword =  $p' < input.json

Upvotes: 1

Related Questions