summaryrefslogtreecommitdiffstats
path: root/tutorial/index.html
blob: fee08d54227775fd8eabad96ceddeb4fbc56c94f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
<!DOCTYPE html>
<html lang="en">
  <head>
  <meta charset=UTF-8>
  <title>Tutorial</title>
  <link rel="stylesheet" href="/jq/bootstrap/css/bootstrap.css" type="text/css">
  <link rel="stylesheet" href="/jq/css/base.css" type="text/css">
  <!--[if IE]>
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  <![endif]-->
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
  <script src="/jq/bootstrap/js/bootstrap.js"></script>

</head>
  <body id="tutorial">
    
<div class="navbar navbar-inverse navbar-fixed-top">
  <div class="navbar-inner">
    <div class="container">
      <button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="brand" href="/jq/">jq</a>
      <div class="nav-collapse collapse">
        <ul class="nav">
          
            <li  class="active" >
              <a href="/jq/tutorial/">Tutorial</a>
            </li>
          
            <li >
              <a href="/jq/download/">Download</a>
            </li>
          
            <li >
              <a href="/jq/manual/">Manual</a>
            </li>
          
          <li><a href="https://github.com/stedolan/jq/issues">Issues</a></li>
          <li><a href="https://github.com/stedolan/jq">Source</a></li>
          <li><a href="http://jqplay.org">Try online</a></li>
        </ul>
      </div>
    </div>
  </div>
</div>
	
    
    <div class="container">
        
      <div class="row">
        <h1>Tutorial</h1>
        
          
            <p>GitHub has a JSON API, so let&#8217;s play with that. This URL gets us the last 5 commits from the jq repo:</p>
          
        
          
        
          
        
          

        
            
            <div class="tutorial-example">
              <div class="accordion-heading">
                <pre>curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5'</pre>
                <a data-toggle="collapse" href="#result1">Show result</a>
              </div>
              <div id="result1" class="accordion-body collapse"><pre>[
  {
    "sha": "d25341478381063d1c76e81b3a52e0592a7c997f",
    "commit": {
      "author": {
        "name": "Stephen Dolan",
        "email": "mu@netsoc.tcd.ie",
        "date": "2013-06-22T16:30:59Z"
      },
      "committer": {
        "name": "Stephen Dolan",
        "email": "mu@netsoc.tcd.ie",
        "date": "2013-06-22T16:30:59Z"
      },
      "message": "Merge pull request #162 from stedolan/utf8-fixes\n\nUtf8 fixes. Closes #161",
      "tree": {
        "sha": "6ab697a8dfb5a96e124666bf6d6213822599fb40",
        "url": "https://api.github.com/repos/stedolan/jq/git/trees/6ab697a8dfb5a96e124666bf6d6213822599fb40"
      },
      "url": "https://api.github.com/repos/stedolan/jq/git/commits/d25341478381063d1c76e81b3a52e0592a7c997f",
      "comment_count": 0
    },
    "url": "https://api.github.com/repos/stedolan/jq/commits/d25341478381063d1c76e81b3a52e0592a7c997f",
    "html_url": "https://github.com/stedolan/jq/commit/d25341478381063d1c76e81b3a52e0592a7c997f",
    "comments_url": "https://api.github.com/repos/stedolan/jq/commits/d25341478381063d1c76e81b3a52e0592a7c997f/comments",
    "author": {
      "login": "stedolan",
    ...
</pre></div>
            </div>
          
        
          
            <p>Github returns a ton of info here, and it&#8217;s relatively hard to read as it is. To syntax highlight it and order the attributes we pipe it through jq, telling jq to just spit the input back at us using the expression <code>.</code>:</p>
          
        
          
        
          
        
          

        
            
            <div class="tutorial-example">
              <div class="accordion-heading">
                <pre>curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq '.'</pre>
                <a data-toggle="collapse" href="#result2">Show result</a>
              </div>
              <div id="result2" class="accordion-body collapse"><pre>[
  {
    "parents": [
      {
        "html_url": "https://github.com/stedolan/jq/commit/54b9c9bdb225af5d886466d72f47eafc51acb4f7",
        "url": "https://api.github.com/repos/stedolan/jq/commits/54b9c9bdb225af5d886466d72f47eafc51acb4f7",
        "sha": "54b9c9bdb225af5d886466d72f47eafc51acb4f7"
      },
      {
        "html_url": "https://github.com/stedolan/jq/commit/8b1b503609c161fea4b003a7179b3fbb2dd4345a",
        "url": "https://api.github.com/repos/stedolan/jq/commits/8b1b503609c161fea4b003a7179b3fbb2dd4345a",
        "sha": "8b1b503609c161fea4b003a7179b3fbb2dd4345a"
      }
    ],
    "committer": {
      "type": "User",
      "received_events_url": "https://api.github.com/users/stedolan/received_events",
      "events_url": "https://api.github.com/users/stedolan/events{/privacy}",
      "repos_url": "https://api.github.com/users/stedolan/repos",
      "organizations_url": "https://api.github.com/users/stedolan/orgs",
...
</pre></div>
            </div>
          
        
          
            <p>There&#8217;s still far too much info for our purposes, so we can try just grab the first commit:</p>
          
        
          
        
          
        
          

        
            
            <div class="tutorial-example">
              <div class="accordion-heading">
                <pre>curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq '.[0]'</pre>
                <a data-toggle="collapse" href="#result3">Show result</a>
              </div>
              <div id="result3" class="accordion-body collapse"><pre>{
  "parents": [
    {
      "html_url": "https://github.com/stedolan/jq/commit/54b9c9bdb225af5d886466d72f47eafc51acb4f7",
      "url": "https://api.github.com/repos/stedolan/jq/commits/54b9c9bdb225af5d886466d72f47eafc51acb4f7",
      "sha": "54b9c9bdb225af5d886466d72f47eafc51acb4f7"
    },
    {
      "html_url": "https://github.com/stedolan/jq/commit/8b1b503609c161fea4b003a7179b3fbb2dd4345a",
      "url": "https://api.github.com/repos/stedolan/jq/commits/8b1b503609c161fea4b003a7179b3fbb2dd4345a",
      "sha": "8b1b503609c161fea4b003a7179b3fbb2dd4345a"
    }
  ],
  "committer": {
    "type": "User",
    "received_events_url": "https://api.github.com/users/stedolan/received_events",
    "events_url": "https://api.github.com/users/stedolan/events{/privacy}",
    "repos_url": "https://api.github.com/users/stedolan/repos",
    "organizations_url": "https://api.github.com/users/stedolan/orgs",
    "subscriptions_url": "https://api.github.com/users/stedolan/subscriptions",
    "starred_url": "https://api.github.com/users/stedolan/starred{/owner}{/repo}",
    "gists_url": "https://api.github.com/users/stedolan/gists{/gist_id}",
    "login": "stedolan",
    "id": 79765,
    "avatar_url": "https://1.gravatar.com/avatar/31de909d8e55dd07ed782d92ece59842?d=https%3A%2F%2Fidenticons.github.com%2Ffc5b6765b1c9cfaecea48ae71df4d279.png",
    "gravatar_id": "31de909d8e55dd07ed782d92ece59842",
    "url": "https://api.github.com/users/stedolan",
    "html_url": "https://github.com/stedolan",
    "followers_url": "https://api.github.com/users/stedolan/followers",
    "following_url": "https://api.github.com/users/stedolan/following{/other_user}"
  },
  "author": {
    "type": "User",
    "received_events_url": "https://api.github.com/users/stedolan/received_events",
    "events_url": "https://api.github.com/users/stedolan/events{/privacy}",
    "repos_url": "https://api.github.com/users/stedolan/repos",
    "organizations_url": "https://api.github.com/users/stedolan/orgs",
    "subscriptions_url": "https://api.github.com/users/stedolan/subscriptions",
    "starred_url": "https://api.github.com/users/stedolan/starred{/owner}{/repo}",
    "gists_url": "https://api.github.com/users/stedolan/gists{/gist_id}",
    "login": "stedolan",
    "id": 79765,
    "avatar_url": "https://1.gravatar.com/avatar/31de909d8e55dd07ed782d92ece59842?d=https%3A%2F%2Fidenticons.github.com%2Ffc5b6765b1c9cfaecea48ae71df4d279.png",
    "gravatar_id": "31de909d8e55dd07ed782d92ece59842",
    "url": "https://api.github.com/users/stedolan",
    "html_url": "https://github.com/stedolan",
    "followers_url": "https://api.github.com/users/stedolan/followers",
    "following_url": "https://api.github.com/users/stedolan/following{/other_user}"
  },
  "comments_url": "https://api.github.com/repos/stedolan/jq/commits/d25341478381063d1c76e81b3a52e0592a7c997f/comments",
  "html_url": "https://github.com/stedolan/jq/commit/d25341478381063d1c76e81b3a52e0592a7c997f",
  "url": "https://api.github.com/repos/stedolan/jq/commits/d25341478381063d1c76e81b3a52e0592a7c997f",
  "commit": {
    "comment_count": 0,
    "url": "https://api.github.com/repos/stedolan/jq/git/commits/d25341478381063d1c76e81b3a52e0592a7c997f",
    "tree": {
      "url": "https://api.github.com/repos/stedolan/jq/git/trees/6ab697a8dfb5a96e124666bf6d6213822599fb40",
      "sha": "6ab697a8dfb5a96e124666bf6d6213822599fb40"
    },
    "message": "Merge pull request #162 from stedolan/utf8-fixes\n\nUtf8 fixes. Closes #161",
    "committer": {
      "date": "2013-06-22T16:30:59Z",
      "email": "mu@netsoc.tcd.ie",
      "name": "Stephen Dolan"
    },
    "author": {
      "date": "2013-06-22T16:30:59Z",
      "email": "mu@netsoc.tcd.ie",
      "name": "Stephen Dolan"
    }
  },
  "sha": "d25341478381063d1c76e81b3a52e0592a7c997f"
}
</pre></div>
            </div>
          
        
          
            <p>For the rest of the examples, I&#8217;ll leave out the <code>curl</code> command - it&#8217;s not going to change.</p>

<p>There&#8217;s still a lot of info we don&#8217;t care about there, so we&#8217;ll restrict it down to the most interesting fields.</p>
          
        
          
        
          
        
          

        
            
            <div class="tutorial-example">
              <div class="accordion-heading">
                <pre>jq '.[0] | {message: .commit.message, name: .commit.committer.name}'</pre>
                <a data-toggle="collapse" href="#result4">Show result</a>
              </div>
              <div id="result4" class="accordion-body collapse"><pre>{
  "name": "Stephen Dolan",
  "message": "Merge pull request #162 from stedolan/utf8-fixes\n\nUtf8 fixes. Closes #161"
}
</pre></div>
            </div>
          
        
          
            <p>The <code>|</code> operator in jq feeds the output of one filter (<code>.[0]</code> which gets the first element of the array in the response) into the input of another (<code>{...}</code> which builds an object out of those fields). You can access nested attributes, such as <code>.commit.message</code>:</p>

<p>Now let&#8217;s get the rest of the commits:</p>
          
        
          
        
          
        
          

        
            
            <div class="tutorial-example">
              <div class="accordion-heading">
                <pre>jq '.[] | {message: .commit.message, name: .commit.committer.name}'</pre>
                <a data-toggle="collapse" href="#result5">Show result</a>
              </div>
              <div id="result5" class="accordion-body collapse"><pre>{
  "name": "Stephen Dolan",
  "message": "Merge pull request #162 from stedolan/utf8-fixes\n\nUtf8 fixes. Closes #161"
}
{
  "name": "Stephen Dolan",
  "message": "Reject all overlong UTF8 sequences."
}
{
  "name": "Stephen Dolan",
  "message": "Fix various UTF8 parsing bugs.\n\nIn particular, parse bad UTF8 by replacing the broken bits with U+FFFD\nand resychronise correctly after broken sequences."
}
{
  "name": "Stephen Dolan",
  "message": "Fix example in manual for `floor`. See #155."
}
{
  "name": "Nicolas Williams",
  "message": "Document floor"
}
</pre></div>
            </div>
          
        
          
            <p><code>.[]</code> returns each element of the array returned in the response, one at a time, which are all fed into <code>{message: .commit.message, name: .commit.committer.name}</code>.</p>

<p>Data in jq is represented as streams of JSON values - every jq expression runs for each value in its input stream, and can produce any number of values to its output stream.</p>

<p>Streams are serialised by just separating JSON values with whitespace. This is a <code>cat</code>-friendly format - you can just join two JSON streams together and get a valid JSON stream.</p>

<p>If you want to get the output as a single array, you can tell jq to &#8220;collect&#8221; all of the answers by wrapping the filter in square brackets:</p>
          
        
          
        
          
        
          

        
            
            <div class="tutorial-example">
              <div class="accordion-heading">
                <pre>jq '[.[] | {message: .commit.message, name: .commit.committer.name}]'</pre>
                <a data-toggle="collapse" href="#result6">Show result</a>
              </div>
              <div id="result6" class="accordion-body collapse"><pre>[
  {
    "name": "Stephen Dolan",
    "message": "Merge pull request #162 from stedolan/utf8-fixes\n\nUtf8 fixes. Closes #161"
  },
  {
    "name": "Stephen Dolan",
    "message": "Reject all overlong UTF8 sequences."
  },
  {
    "name": "Stephen Dolan",
    "message": "Fix various UTF8 parsing bugs.\n\nIn particular, parse bad UTF8 by replacing the broken bits with U+FFFD\nand resychronise correctly after broken sequences."
  },
  {
    "name": "Stephen Dolan",
    "message": "Fix example in manual for `floor`. See #155."
  },
  {
    "name": "Nicolas Williams",
    "message": "Document floor"
  }
]
</pre></div>
            </div>
          
        
          
            <hr />
<p>Next, let&#8217;s try getting the URLs of the parrent commits out of the API results as well. In each commit, the GitHub API includes information about &#8220;parent&#8221; commits. There can be one or many.</p>

<pre><code>&quot;parents&quot;: [
  {
    &quot;html_url&quot;: &quot;https://github.com/stedolan/jq/commit/54b9c9bdb225af5d886466d72f47eafc51acb4f7&quot;,
    &quot;url&quot;: &quot;https://api.github.com/repos/stedolan/jq/commits/54b9c9bdb225af5d886466d72f47eafc51acb4f7&quot;,
    &quot;sha&quot;: &quot;54b9c9bdb225af5d886466d72f47eafc51acb4f7&quot;
  },
  {
    &quot;html_url&quot;: &quot;https://github.com/stedolan/jq/commit/8b1b503609c161fea4b003a7179b3fbb2dd4345a&quot;,
    &quot;url&quot;: &quot;https://api.github.com/repos/stedolan/jq/commits/8b1b503609c161fea4b003a7179b3fbb2dd4345a&quot;,
    &quot;sha&quot;: &quot;8b1b503609c161fea4b003a7179b3fbb2dd4345a&quot;
  }
]</code></pre>

<p>We want to pull out all of the &#8220;html_url&#8221; fields inside that array of parent commits and make a simple list of strings to go along with the &#8220;message&#8221; and &#8220;author&#8221; fields we already have.</p>
          
        
          
        
          
        
          

        
            
            <div class="tutorial-example">
              <div class="accordion-heading">
                <pre>jq '[.[] | {message: .commit.message, name: .commit.committer.name, parents: [.parents[].html_url]}]'</pre>
                <a data-toggle="collapse" href="#result7">Show result</a>
              </div>
              <div id="result7" class="accordion-body collapse"><pre>[
  {
    "parents": [
      "https://github.com/stedolan/jq/commit/54b9c9bdb225af5d886466d72f47eafc51acb4f7",
      "https://github.com/stedolan/jq/commit/8b1b503609c161fea4b003a7179b3fbb2dd4345a"
    ],
    "name": "Stephen Dolan",
    "message": "Merge pull request #162 from stedolan/utf8-fixes\n\nUtf8 fixes. Closes #161"
  },
  {
    "parents": [
      "https://github.com/stedolan/jq/commit/ff48bd6ec538b01d1057be8e93b94eef6914e9ef"
    ],
    "name": "Stephen Dolan",
    "message": "Reject all overlong UTF8 sequences."
  },
  {
    "parents": [
      "https://github.com/stedolan/jq/commit/54b9c9bdb225af5d886466d72f47eafc51acb4f7"
    ],
    "name": "Stephen Dolan",
    "message": "Fix various UTF8 parsing bugs.\n\nIn particular, parse bad UTF8 by replacing the broken bits with U+FFFD\nand resychronise correctly after broken sequences."
  },
  {
    "parents": [
      "https://github.com/stedolan/jq/commit/3dcdc582ea993afea3f5503a78a77675967ecdfa"
    ],
    "name": "Stephen Dolan",
    "message": "Fix example in manual for `floor`. See #155.&q