$linuxjunkies
>

slurp(1)

Read entire input into a JSON array.

UbuntuDebianFedoraArch

Synopsis

slurp [OPTION]... [FILE]...

Description

slurp is a jq-based tool that reads JSON inputs from files or stdin and combines them into a single JSON array. Instead of processing JSON objects one per line, slurp accumulates all inputs and outputs them as an array, making it easy to process multiple JSON records as a collection.

By default, slurp reads JSON values separated by whitespace or newlines and wraps them in an array. If no files are specified, it reads from standard input.

Common options

FlagWhat it does
-rRead raw strings instead of JSON; useful for slurping lines of text
-sSlurp input; equivalent to wrapping the filter in [.]
-nUse null input; don't read any input
-cCompact output (no pretty-printing)
-RRead raw input lines as strings
--tabUse tabs for indentation instead of spaces

Examples

Combine two separate JSON objects into a single array

echo -e '{"a":1}\n{"b":2}' | slurp

Read and slurp multiple JSON files into one array

jq -s '.' file1.json file2.json file3.json

Slurp a JSON Lines file and extract the 'name' field from each object

cat data.jsonl | jq -s 'map(.name)'

Slurp raw lines from ls output and filter empty lines

ls -1 | jq -sR 'split("\n") | map(select(length > 0))'

Count the total number of JSON objects across multiple files

slurp -c *.json | jq 'length'

Slurp records and group them by type with counts

jq -s 'group_by(.type) | map({type: .[0].type, count: length})' records.json

Related commands