Read File into JSON List with jq
You have a file and you want to convert the contents into a JSON list. How can
jq to do that?
$ cat file one two three $ jq --null-input --raw-input '[inputs]' [ "one", "two", "three" ]
-R) takes each line in the input as a string and passes
it to the filter, instead of interpreting the file as containing JSON.
-n) is important:
$ printf '%s\n' one two three | jq --raw-input '[inputs]' [ "two", "three" ]
--null-input doesn’t read the input at all. Instead, it will run the filter
null as input. In our case,
inputs here, which triggers
an I/O operation that reads the contents of the file (or files) as strings
--raw-input) into a list (because
inputs is surrounded by
jq takes the first line of the file, and passes that
to the filter. The filter discards this input (we don’t use the identity
.) and proceeds to read all remaining inputs (
Obviously, this is not what we want.
Knowing this, we could do the following (but really shouldn’t):
$ printf '%s\n' one two three | jq --raw-input '[.] + [inputs]' [ "one", "two", "three" ]
Remove Empty Strings from Output
If you get empty strings for whatever reason in your output, you can remove them:
$ printf '%s\n' '' one two three | jq -n -R '[inputs]' [ "", "one", "two", "three" ] $ printf '%s\n' '' one two three | jq -n -R '[inputs | select(length > 0)]' [ "one", "two", "three" ]