如何将 JSON 转换为环境变量文件格式?
How can I convert JSON to an environment variable file format?
我正在使用 AWS cli 获取 AWS Parameter Store JSON 响应:
echo $(aws ssm get-parameters-by-path --with-decryption --region eu-central-1 \
--path "${PARAMETER_STORE_PREFIX}") >/home/ubuntu/.env.json
输出如下:
{
"Parameters": [
{
"Name": "/EXAMPLE/CORS_ORIGIN",
"Value": "https://example.com"
},
{
"Name": "/EXAMPLE/DATABASE_URL",
"Value": "db://user:pass@host:3306/example"
}
]
}
我不想写入 JSON 文件,而是希望使用以下格式写入 .env
文件:
CORS_ORIGIN="https://example.com"
DATABASE_URL="db://user:pass@host:3306/example"
我怎样才能做到这一点?我发现类似 的问题,但它们不处理嵌套 JSON / 数组
使用 jq
和一些字符串插值以及 @sh
可以轻松地正确引用 shell 的值字符串:
$ jq -r '.Parameters[] | "\(.Name | .[rindex("/")+1:])=\(.Value | @sh)"' input.json
CORS_ORIGIN='https://example.com'
DATABASE_URL='db://user:pass@host:3306/example'
我正在使用 AWS cli 获取 AWS Parameter Store JSON 响应:
echo $(aws ssm get-parameters-by-path --with-decryption --region eu-central-1 \
--path "${PARAMETER_STORE_PREFIX}") >/home/ubuntu/.env.json
输出如下:
{
"Parameters": [
{
"Name": "/EXAMPLE/CORS_ORIGIN",
"Value": "https://example.com"
},
{
"Name": "/EXAMPLE/DATABASE_URL",
"Value": "db://user:pass@host:3306/example"
}
]
}
我不想写入 JSON 文件,而是希望使用以下格式写入 .env
文件:
CORS_ORIGIN="https://example.com"
DATABASE_URL="db://user:pass@host:3306/example"
我怎样才能做到这一点?我发现类似
使用 jq
和一些字符串插值以及 @sh
可以轻松地正确引用 shell 的值字符串:
$ jq -r '.Parameters[] | "\(.Name | .[rindex("/")+1:])=\(.Value | @sh)"' input.json
CORS_ORIGIN='https://example.com'
DATABASE_URL='db://user:pass@host:3306/example'