5

This generates a list of arrays:

$ echo -e "a 1\nb 2" | jq -R 'split(" ")'
[
  "a",
  "1"
]
[
  "b",
  "2"
]

When I slurp the input I get an array:

$ echo -e "a 1\nb 2" | jq -R 'split(" ")' | jq -s .
[
  [
    "a",
    "1"
  ],
  [
    "b",
    "2"
  ]
]

But when I try to convert the list into an array without slurping it, I get a list of arrays instead of a single array:

$ echo -e "a 1\nb 2" | jq -R '[split(" ")]'
[
  [
    "a",
    "1"
  ]
]
[
  [
    "b",
    "2"
  ]
]

Is it possible to slurp the result of the split without piping the result into a new instance of jq?

peak
  • 105,803
  • 17
  • 152
  • 177
ceving
  • 21,900
  • 13
  • 104
  • 178

2 Answers2

4

Before the advent of inputs, the answer to the question was "No". With inputs and the -n command-line option:

$ echo -e "a 1\nb 2" | jq -nR '[inputs|split(" ")]' 
[
  [
    "a",
    "1"
  ],
  [
    "b",
    "2"
  ]
]
peak
  • 105,803
  • 17
  • 152
  • 177
-1

With double split:

echo -e "a 1\nb 2" | jq -sR 'split("\n")[:-1] | map(split(" "))'

The output:

[
  [
    "a",
    "1"
  ],
  [
    "b",
    "2"
  ]
]
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
  • 4
    I know I can use the slurp option. But I was asking, if something equivalent exists as a `jq` expression. – ceving Jul 02 '18 at 09:42