Issue
I am running an echo command in bash and I am seeing some unexpected results.
When running:
echo "{
"outputs": {
"result": "[{\"directory\":\"mydir\",\"somekey\":\"somevalue\"}]"
},
"outcome": "success",
"conclusion": "success"
}"
The output is:
{
outputs: {
result: ["directory":"mydir"]
},
outcome: success,
conclusion: success
} {
outputs: {
result: ["somekey":"somevalue"]
},
outcome: success,
conclusion: success
}
I understand that the result key is not valid json but why does the output seem to iterate of the values in the result array and output the objects? What causes this in the bash or echo command side?
If I remove the double quotes on from the beginning and the end of the result array, it outputs the entire json blob as expected but curious why I get the other output when json is malformed.
Solution
What you see is called brace expansion. At its most simple, it looks like
$ echo {a,b}
a b
If the braces are embedded in a string, the entire string gets repeated:
$ echo prefix{a,b}suffix
prefixasuffix prefixbsuffix
Importantly, when the braces are quoted, the expansion doesn't happen:
$ echo "prefix{a,b}suffix"
prefix{a,b}suffix
In your case, prefix
is
"{
"outputs": {
"result": "[
a
is
\"directory\":\"mydir\"
b
is
\"somekey\":\"somevalue\"
and suffix
is
]"
},
"outcome": "success",
"conclusion": "success"
}"
Answered By - Benjamin W. Answer Checked By - Marie Seifert (WPSolving Admin)