Jq: Raw lines to an array of lines?

Created on 29 Aug 2014  路  6Comments  路  Source: stedolan/jq

Is there a way to convert multiline, non-JSON input to a JSON array of lines in one invocation of jq?

Desired result:

echo -en "foo\nbar\nbaz" | jq <unknown set of options and expression>
[
  "foo",
  "bar",
  "baz"
]

Failed attempts:

$ echo -en "foo\nbar\nbaz" | jq --raw-input '[.]'
[
  "foo"
]
[
  "bar"
]
[
  "baz"
]
$ echo -en "foo\nbar\nbaz" | jq --raw-input --slurp '[.]'
[
  "foo\nbar\nbaz"
]

I did manage to get the result that I want, but only by using --raw-input and --slurp separately:

$ echo -en "foo\nbar\nbaz" | jq --raw-input . | jq --slurp .
[
  "foo",
  "bar",
  "baz"
]

Is that the preferred way?

support

Most helpful comment

If your jq has inputs then to avoid having to read the whole file into memory up front, it would be better to use it, e.g. if empty lines are to be omitted:

jq -nR '[inputs | select(length>0)]'

All 6 comments

Use split/1:

$ jq --raw-input --slurp 'split("\n")' query.txt
[
  "foo",
  "bar",
  "baz",
  ""
]

But as shown above you might have to be careful about trailing carriage returns.

Thanks for the tip.

| .[0:-1] will remove that last empty element.

jq --raw-input --slurp 'split("\n") | map(select(. != ""))' query.txt
removes all the blank lines, assuming that's what you want to do.

If your jq has inputs then to avoid having to read the whole file into memory up front, it would be better to use it, e.g. if empty lines are to be omitted:

jq -nR '[inputs | select(length>0)]'

How about using the --args flag:

$ jq -n '$ARGS.positional' --args foo bar baz
[
  "foo",
  "bar",
  "baz"
]

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rokka-n picture rokka-n  路  4Comments

tbelaire picture tbelaire  路  4Comments

thelonious picture thelonious  路  4Comments

rubensayshi picture rubensayshi  路  3Comments

neowulf picture neowulf  路  3Comments