summaryrefslogtreecommitdiff
path: root/Using JQ to construct an array and filtering empty strings.md
blob: d04c59336163422983a0305b0d694b3369513dd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
---
tags:
  - jq
  - howto
---
Today I learned how to use JQ to construct an array from stdin, while also filtering empty strings.

In below examples the -R switch makes jq process input as raw non-json data, and the -n switch stops it from discarding the first line.

```
sudo virsh checkpoint-list --name instance-0002c4d7 | jq -Rn '[inputs | if length > 0 then . else empty end]'
```
^ Demonstrates use of empty

```
sudo virsh checkpoint-list --name instance-0002c4d7 | jq -Rn '[inputs | select(length > 0)]'
```
^ easier using select