CircleCI Field Guide
GitHub Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage
Edit page

convert JSON values to ENV VAR

This is a trick that @eddiewebb finds handy to converry Hashicorp CLI responses into local environment variables that can be used in subequent commands (docker login,etc)

Prerequsities

  • an image with jq (all CCI images have it)
  • JSON respresenting values to convert to ENV VARs

Approach

We basically use jq as a rudimentary templating engine, converting existing k/v to shell format.

Code

Sample Payload

{
    "data":
    {
        "username":"agent_007",
        "password":"SuperSocks",
        "pin": "0000"
    }
}

Result

export AGENT_USERNAME='agent_007'
export AGENT_PASSWORD='SuperSocks'
export AGENT_PIN='0000'

JQ Command

jq -r '.data | to_entries[] | "export AGENT_"+(.key | ascii_upcase)+"="+(.value | @sh)' >> $BASH_ENV
source $BASH_ENV # only if needed in this step, evrry future step will load for us

Try it Out

https://jqplay.org/s/3RlH6OtQKfF

Screenshot of JQ playground converting json into shell variables