skip to Main Content

My messages generator output

$ ./messages.sh
{"a":"v1"}
{"b":"v2"}
{"c":"v3"}
...

Required output

$ ./messages.sh | jq xxxxxx
[{"a":"v1"},{"b":"v2"}]
[{"c":"v3"},{"d":"v4"}]
...

3

Answers


  1. Take the first item using ., and the second using input (prepended by try to handle cases of not enough input items). Then, wrap them both into array brackets, and provide the -c option for compact output. jq will work through its whole input one by one (or two by two).

    ./messages.sh | jq -c '[., try input]'
    
    [{"a":"v1"},{"b":"v2"}]
    [{"c":"v3"},{"d":"v4"}]
    

    What if I want more objects in the array than 2? For example, 3, 10, 100?

    You can surround the array body with limit, and use inputs instead (note the s) to fetch more than just one item:

    ./messages.sh | jq -c '[limit(3; ., try inputs)]'
    
    [{"a":"v1"},{"b":"v2"},{"c":"v3"}]
    [{"d":"v4"}]
    
    Login or Signup to reply.
  2. Use slurp with _nwise(2) to chunk into parts of 2:

    jq --slurp --compact-output '_nwise(2)' <<< $(./messages.sh)
    
    [{"a":"v1"},{"b":"v2"}]
    [{"c":"v3"},{"d":"v4"}]
    

    The --compact-output is to output each array on a single line

    Login or Signup to reply.
  3. Here is a stream-oriented, generic and portable def of nwise:

    # Group the items in the given stream into arrays of up to length $n
    # assuming $n is a non-negative integer > 0
    # Input: a stream
    # Output: a stream of arrays no longer than $n
    # such that [stream] == ([ nwise(stream; $n) ] | add)
    # Notice that there is no assumption about an eos marker.
    def nwise(stream; $n):
      foreach ((stream | [.]), null) as $x ([];
        if $x == null then . 
        elif length == $n then $x
        else . + $x end;
        if $x == null and length>0 then .
        elif length==$n then .
        else empty
        end);
    

    For the task at hand, you could use it like so:

    nwise(inputs; 2)
    

    with the -n command-line option.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search