I have the following json output
{
"secrets": [
[
"{",
" "name": "testSecret",",
" "vault-apikey": "kv",",
" "vault-value": "sabrinaTest"",
"}"
]
]
}
But I need the output to be a little cleaner and look like this:
{
"secrets": [
[
{
"name" : "testSecret",
"vault-apikey" : "kv",
"vault-value" : "sabrinaTest"
}
]
]
}
These are the commands that I’m currently running to get this output:
TEST_SECRET_JSON=$(hcp vault-secrets secrets open testSecret --format=json)
SECRETS_INFO=$(echo $TEST_SECRET_JSON | jq -r '{"name": .name, "vault-apikey": .type, "vault-value": .static_version.value}')
jq -n --arg value "${SECRETS_INFO}" '{ secrets : [ $value | split("n") ] }' >> secret.json
I have tried other variations, but none have produced the desired results.
2
Answers
SECRETS_INFO
is already ajq
object; use--argjson
instead of--arg
.Or, just pipe the object into
jq
as input.Issue is the way you are handling
jq
to handle the formatting. You’re splitting the JSON string into separate lines, but what you need is to parse and preserve it as an object. Here is a better approachjq
to create the desired JSON structure directly, without splitting it to two lines.