jq.md(файл создан)
@@ -0,0 +1,77 @@ | |||
1 | + | # Processing JSON using jq | |
2 | + | ||
3 | + | jq is useful to slice, filter, map and transform structured json data. | |
4 | + | ||
5 | + | ## Useful arguments | |
6 | + | ||
7 | + | When running jq, the following arguments may become handy: | |
8 | + | ||
9 | + | | Argument | Description | | |
10 | + | | ----------------| :------------:| | |
11 | + | | `--version`| Output the jq version and exit with zero. | | |
12 | + | | `--sort-keys` | Output the fields of each object with the keys in sorted order.| | |
13 | + | ||
14 | + | ## Basic concepts | |
15 | + | ||
16 | + | The syntax for jq is pretty coherent: | |
17 | + | ||
18 | + | | Syntax | Description | | |
19 | + | | --------| :------------:| | |
20 | + | | , | Filters separated by a comma will produce multiple independent outputs| | |
21 | + | | ? | Will ignores error if the type is unexpected | | |
22 | + | | [] | Array construction | | |
23 | + | | {} | Object construction | | |
24 | + | | + | Concatenate or Add | | |
25 | + | | - | Difference of sets or Substract | | |
26 | + | | length | Size of selected element | | |
27 | + | | | | Pipes are used to chain commands in a similar fashion than bash| | |
28 | + | ||
29 | + | ||
30 | + | ## Dealing with json objects | |
31 | + | ||
32 | + | | Description | Command | | |
33 | + | | ------------| :-----: | | |
34 | + | | Display all keys | `jq 'keys'` | | |
35 | + | | Adds + 1 to all items | `jq 'map_values(.+1)'` | | |
36 | + | | Delete a key| `jq 'del(.foo)'` | | |
37 | + | | Convert an object to array | `to_entries | map([.key, .value])` | | |
38 | + | ||
39 | + | ## Dealing with fields | |
40 | + | ||
41 | + | | Description | Command | | |
42 | + | | ------------| :-----: | | |
43 | + | | Concatenate two fields| `fieldNew=.field1+' '+.field2` | | |
44 | + | ||
45 | + | ||
46 | + | ## Dealing with json arrays | |
47 | + | ||
48 | + | ### Slicing and Filtering | |
49 | + | ||
50 | + | | Description | Command | | |
51 | + | | ------------| :-----: | | |
52 | + | | All | `jq .[]` | | |
53 | + | | First | `jq '.[0]'` | | |
54 | + | | Range | `jq '.[2:4]'` | | |
55 | + | | First 3 | `jq '.[:3]'` | | |
56 | + | | Last 2 | `jq '.[-2:]'` | | |
57 | + | | Before Last | `jq '.[-2]'`| | |
58 | + | | Select array of int by value | `jq 'map(select(. >= 2))'` | | |
59 | + | | Select array of objects by value| ** jq '.[] | select(.id == "second")'** | | |
60 | + | | Select by type | ** jq '.[] | numbers' ** with type been arrays, objects, iterables, booleans, numbers, normals, finites, strings, nulls, values, scalars | | |
61 | + | ||
62 | + | ### Mapping and Transforming | |
63 | + | ||
64 | + | | Description | Command | | |
65 | + | | ------------| :-----: | | |
66 | + | | Add + 1 to all items | `jq 'map(.+1)'` | | |
67 | + | | Delete 2 items| `jq 'del(.[1, 2])'` | | |
68 | + | | Concatenate arrays | `jq 'add'` | | |
69 | + | | Flatten an array | `jq 'flatten'` | | |
70 | + | | Create a range of numbers | `jq '[range(2;4)]'` | | |
71 | + | | Display the type of each item| `jq 'map(type)'` | | |
72 | + | | Sort an array of basic type| `jq 'sort'` | | |
73 | + | | Sort an array of objects | `jq 'sort_by(.foo)'` | | |
74 | + | | Group by a key - opposite to flatten | `jq 'group_by(.foo)'` | | |
75 | + | | Minimun value of an array| `jq 'min'` .See also min, max, min_by(path_exp), max_by(path_exp) | | |
76 | + | | Remove duplicates| `jq 'unique'` or `jq 'unique_by(.foo)'` or `jq 'unique_by(length)'` | | |
77 | + | | Reverse an array | `jq 'reverse'` | |
Новее
Позже