Relentless Coding

A Developer’s Blog

Read File into JSON List with jq

You have a file and you want to convert the contents into a JSON list. How can we leverage jq to do that?

$ cat file
one
two
three
$ jq --null-input --raw-input '[inputs]'
[
  "one",
  "two",
  "three"
]

--raw-input (short -R) takes each line in the input as a string and passes it to the filter, instead of interpreting the file as containing JSON.

--null-input (short -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 with null as input. In our case, jq encounters inputs here, which triggers an I/O operation that reads the contents of the file (or files) as strings (because of --raw-input) into a list (because inputs is surrounded by [...]).

Without --null-input, 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 operator .) and proceeds to read all remaining inputs (two and three). 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"
]