I have a space separated list of names generated by a program, I need to convert this list to json. I can not change the program generating the file output. I need to convert the output in file names.txt to json.
example: file output is called names.txt. context is below.
name1 name2 name3
The json expectation is
{
"names": [
"name1",
"name2",
"name3"
]
}
A jq solution could be simplefied by passing the 'string' as argument, then using split()
to create an array like so:
jq -n --arg d "$(cat input)" '{ "names": $d | split(" ") }'
Local example:
$ cat input
name1 name2 name3
$
$ jq -n --arg d "$(cat input)" '{ "names": $d | split(" ") }'
{
"names": [
"name1",
"name2",
"name3"
]
}
$
$