refactor: finish moving ssh-* scripts to own installers
[webi-installers/.git] / jq / README.md
1 ---
2 title: jq
3 homepage: https://stedolan.github.io/jq/
4 tagline: |
5   jq is a lightweight and flexible command-line JSON processor.
6 ---
7
8 To update or switch versions, run `webi jq@stable` (or `@v1.6`, `@beta`, etc).
9
10 ## Cheat Sheet
11
12 > `jq` is like `sed` for JSON data - you can use it to slice and filter and map
13 > and transform structured data with the same ease that `sed`, `awk`, `grep` and
14 > friends let you play with text.
15
16 All jq selectors begin with `.` - don't forget that!
17
18 Be sure to checkout the
19 [official tutorial](https://stedolan.github.io/jq/tutorial/) and
20 [jq manual](https://stedolan.github.io/jq/manual/) for more info.
21
22 You can also [try online](https://jqplay.org/).
23
24 ### How to select a single a property from an object
25
26 ```bash
27 echo '{ "name": "foo" }' | jq '.name'
28 ```
29
30 ```txt
31 "foo"
32 ```
33
34 ### How to remove quotes from strings
35
36 The `-r` or `--raw-output` flag unwraps strings:
37
38 ```bash
39 echo '{ "name": "foo" }' | jq -r '.name'
40 ```
41
42 ```txt
43 foo
44 ```
45
46 ### How to select a whole object
47
48 ```bash
49 echo '{ "name": "foo" }' | jq '.'
50 ```
51
52 ```txt
53 {
54   "name": "foo"
55 }
56 ```
57
58 ### How to select an element from an array
59
60 ```bash
61 echo '[ { "name": "foo" } ]' | jq '.[0]'
62 ```
63
64 ```txt
65 {
66   "name": "foo"
67 }
68 ```
69
70 ### How to select a single property from an array element
71
72 ```bash
73 echo '[ { "name": "foo" } ]' | jq -r '.[0].name'
74 ```
75
76 ```txt
77 foo
78 ```
79
80 ### How to select some properties from multiple elements
81
82 ```bash
83 echo '[ { "name": "foo" }, { "name": "bar" } ]' \
84     | jq -r '.[].name'
85 ```
86
87 ```txt
88 foo
89 bar
90 ```
91
92 ### How transform or zip an array
93
94 Anything that doesn't start with a `.` is part of the transformation template.
95
96 Anything that collects starts with `.[]`.
97
98 Anything that transforms has a pipe and selector `| .whatever`.
99
100 Be sure to checkout the
101 [official tutorial](https://stedolan.github.io/jq/tutorial/) and
102 [jq manual](https://stedolan.github.io/jq/manual/) for more info.
103
104 ```bash
105 echo '[ { "name": "foo", "age": 0 }, { "name": "bar", "age": 2 } ]' \
106     | jq '{ names: [.[] | .name], ages: [.[] | .age] }'
107 ```
108
109 ```txt
110 {
111   "names": [
112     "foo",
113     "bar"
114   ],
115   "ages": [
116     0,
117     2
118   ]
119 }
120 ```