I have the following string in bash
"3.8,3.9,3.10"
Is there a way using shell to convert it into a json
array, i.e.
["3.8", "3.9", "3.10"]
Since the string (including its double quotes) seems to be a properly formatted JSON string, we could pass it, as JSON, to the JSON processor jq
and split it on the commas:
$ echo '"3.8,3.9,3.10"' | jq 'split(",")' [ "3.8", "3.9", "3.10" ]
Use jq
with -c
to get "compact output":
$ echo '"3.8,3.9,3.10"' | jq -c 'split(",")' ["3.8","3.9","3.10"]
Giving it to an internal jq
variable on the command line and then splitting that variable's value:
$ jq -c -n --argjson str '"3.8,3.9,3.10"' '$str|split(",")' ["3.8","3.9","3.10"]