How I can remove empty parameters from jq output without editing and removing a empty spaces from variable?
My output is, and I need remove this empty values like {"{#PARAMETER}":""}:
{"data":[{"{#PARAMETER}":""},{"{#PARAMETER}":"test1"},{"{#PARAMETER}":"test2"},{"{#PARAMETER}":"test3"},{"{#PARAMETER}":"test4"},{"{#PARAMETER}":""}]}
Reproduce script.
#!/bin/bash
TEST="
test1
test2
test3
test4
"
echo -n "${TEST}" | jq -R -s -c '{data: split("\n") | map({"{#PARAMETER}": (.)}) }'
$TEST, or filter the output fromechobefore it gets passed to jq. You don't want to do either of those?printf %s "$var"instead ofecho -n "$var". Depending on which runtime configuration flags are active,echo -ncan just echo-ninstead of suppressing newlines -- and it's perfectly within POSIX specifications to do so! (See in particular the APPLICATION USAGE and RATIONALE sections of pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html, and note all the uses of "implementation-defined" within that spec).echo "\t"can legally replace that\twith a tab character even without a-e; indeed, doing anything other than printing-eas output when it's passed as an argument violates POSIX, which is part of why bash has runtime-configurable flags to change howechobehaves, so it's at least capable of being made strictly-compliant.