Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raw lines to an array of lines? #563

Closed
rfletcher opened this issue Aug 28, 2014 · 9 comments
Closed

Raw lines to an array of lines? #563

rfletcher opened this issue Aug 28, 2014 · 9 comments
Labels

Comments

@rfletcher
Copy link

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?

@pkoppstein
Copy link
Contributor

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.

@rfletcher
Copy link
Author

Thanks for the tip.

@andrewharvey
Copy link

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

@davidfetter
Copy link
Contributor

davidfetter commented Jul 13, 2018

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

@pkoppstein
Copy link
Contributor

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)]'

@scr-oath
Copy link

scr-oath commented Oct 8, 2019

How about using the --args flag:

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

@lorenzogrv
Copy link

@pkoppstein Awesome solution, but I would like to ask:

  1. why --null-input if ussing --raw-input?
  2. Wouldn't jq --raw-input '[inputs]' suffice?

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)]'

@itchyny
Copy link
Contributor

itchyny commented Mar 11, 2021

@lorenzogrv Unless specifying the --null-input (-n) option, the first input value is used as the input to the query. Checkout the difference of seq 3 | jq -nR '., [inputs]' and seq 3 | jq -R '., [inputs]'.

@lorenzogrv
Copy link

@itchyny oh, I see. Thx 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants