slurp(1)
Read entire input into a JSON array.
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
| Flag | What it does |
|---|---|
-r | Read raw strings instead of JSON; useful for slurping lines of text |
-s | Slurp input; equivalent to wrapping the filter in [.] |
-n | Use null input; don't read any input |
-c | Compact output (no pretty-printing) |
-R | Read raw input lines as strings |
--tab | Use tabs for indentation instead of spaces |
Examples
Combine two separate JSON objects into a single array
echo -e '{"a":1}\n{"b":2}' | slurpRead and slurp multiple JSON files into one array
jq -s '.' file1.json file2.json file3.jsonSlurp 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