Using jq, I want to convert the input A B=1 C= D=2
to "A", "B": "1", "C": "", "D": "2"
I came up with the solution
jq -Rr '. / " " | map(. / "=" | "\"\(.[0])\"" (if .[1] != null then ": \"\(.[1])\"" else "" end)) | join(", ")'
I am sure there is a more elegant way that avoids the if-then-else
.
Can you help me shorten the expression?
CodePudding user response:
Try this :
jq -Rr '
. / " " |
map(. / "=" |
"\"" join("\": \"") "\""
) |
join(", ")
' input.json