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