简体   繁体   中英

Convert JSON from AWS SSM to environment variables using jq

I have done some research on this and feel as if I'm about 80% there but struggling to adjust the jq output as required due to splitting one of the strings.

I'm trying to convert the JSON output from AWS SSM to environment variables.

AWS command

aws ssm get-parameters-by-path \
--path /qa/es \
--with-decryption \
--query 'Parameters[*].{Name:Name,Value:Value}' \

Output

[
    {
        "Name": "/qa/es/AWS_ACCESS_KEY_ID",
        "Value": "ABC123"
    },
    {
        "Name": "/qa/es/AWS_SECRET_ACCESS_KEY",
        "Value": "abcdefghijkl"
    },
    {
        "Name": "/qa/es/ENDPOINT",
        "Value": "https://amazonaws.com"
    }
]

My required output from jq, note I'm only after the environment variable AFTER the last /. There may be cases where this could be /qa/es/something/nested/ENV_VAR

AWS_ACCESS_KEY_ID=ABC123
AWS_SECRET_ACCESS_KEY=abcdefghijkl
ENDPOINT=https://amazonaws.com

Once I have this I can utilise the answer here to set the environment variables. Exporting JSON to environment variables

The closest I have got is

jq -r "map(\"\(try(.Name |= split(\"/\")))=\(.Value|tostring)\")|.[]" params.json

Which gives me

{"Name":["","qa","es","AWS_ACCESS_KEY_ID"],"Value":"ABC123"}=ABC123
{"Name":["","qa","es","AWS_SECRET_ACCESS_KEY"],"Value":"abcdefghijkl"}=abcdefghijkl
{"Name":["","qa","es","ENDPOINT"],"Value":"https://amazonaws.com"}=https://amazonaws.com

Close, but not close enough! Can anyone point me in the right direction here?

With the -r command-line option,

.[]
| "\(.Name|split("/")|.[-1])=\(.Value)"

yields:

AWS_ACCESS_KEY_ID=ABC123
AWS_SECRET_ACCESS_KEY=abcdefghijkl
ENDPOINT=https://amazonaws.com

This seems to correspond to what you've asked for, but this approach has the potential disadvantage that it assumes something about "=", so please be careful!

与前面的注释一样,但是使用“ @sh”转义值

| "\(.Name|split("/")|.[-1])=\(.Value | @sh)"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM