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