Skip to content

Commit c922a84

Browse files
committed
Merge branch 'feat/jq-tester' into chore/all-my-stuffs
# Conflicts: # package.json # pnpm-lock.yaml # src/tools/index.ts
2 parents b404cdb + 2bb7b10 commit c922a84

File tree

11 files changed

+435
-5
lines changed

11 files changed

+435
-5
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"@tiptap/vue-3": "2.7.4",
6464
"@types/figlet": "^1.5.8",
6565
"@types/luxon": "^3.4.2",
66+
"@types/jsonpath": "^0.2.4",
6667
"@types/markdown-it": "^13.0.7",
6768
"@types/lodash.defaultsdeep": "^4.6.9",
6869
"@types/lodash.flattendeep": "^4.4.9",
@@ -121,10 +122,12 @@
121122
"ical.js": "^2.1.0",
122123
"image-in-browser": "^3.2.0",
123124
"ip-bigint": "^8.2.0",
125+
"jq-wasm": "^0.0.9",
124126
"js-base64": "^3.7.6",
125127
"image-in-browser": "^3.1.0",
126128
"js-base64": "^3.7.7",
127129
"json5": "^2.2.3",
130+
"jsonpath": "^1.1.1",
128131
"jwt-decode": "^3.1.2",
129132
"libphonenumber-js": "^1.10.28",
130133
"lodash": "^4.17.21",

pnpm-lock.yaml

Lines changed: 91 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/tools/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ import { tool as hexFileConverter } from './hex-file-converter';
1515
import { tool as icalMerger } from './ical-merger';
1616
import { tool as imageConverter } from './image-converter';
1717
import { tool as integersToIp } from './integers-to-ip';
18+
import { tool as jqTester } from './jq-tester';
19+
import { tool as jsonpathMemo } from './jsonpath-memo';
20+
import { tool as jqMemo } from './jq-memo';
1821

1922
import { tool as cssXpathConverter } from './css-xpath-converter';
2023
import { tool as cssSelectorsMemo } from './css-selectors-memo';
@@ -239,6 +242,9 @@ export const toolsByCategory: ToolCategory[] = [
239242
cssSelectorsMemo,
240243
xpathMemo,
241244
curlConverter,
245+
jqTester,
246+
jqMemo,
247+
jsonpathMemo,
242248
],
243249
},
244250
{

src/tools/jq-memo/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Brackets } from '@vicons/tabler';
2+
import { defineTool } from '../tool';
3+
4+
export const tool = defineTool({
5+
name: 'Jq Cheatsheet',
6+
path: '/jq-memo',
7+
description: 'JQ command cheatsheet',
8+
keywords: ['jq', 'cheatsheet', 'memo'],
9+
component: () => import('./jq-memo.vue'),
10+
icon: Brackets,
11+
createdAt: new Date('2024-08-15'),
12+
});

src/tools/jq-memo/jq-memo.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Processing JSON using jq
2+
3+
jq is useful to slice, filter, map and transform structured json data.
4+
5+
## Installing jq
6+
7+
### On Mac OS
8+
9+
`brew install jq`
10+
11+
### On AWS Linux
12+
13+
Not available as yum install on our current AMI. It should be on the latest AMI though: https://aws.amazon.com/amazon-linux-ami/2015.09-release-notes/
14+
15+
Installing from the source proved to be tricky.
16+
17+
## Useful arguments
18+
19+
When running jq, the following arguments may become handy:
20+
21+
| Argument | Description |
22+
| ------ | :--------- |
23+
| `--version`| Output the jq version and exit with zero. |
24+
| `--sort-keys` | Output the fields of each object with the keys in sorted order.|
25+
26+
## Basic concepts
27+
28+
The syntax for jq is pretty coherent:
29+
30+
| Syntax | Description |
31+
| ------ | :--------- |
32+
| , | Filters separated by a comma will produce multiple independent outputs|
33+
| ? | Will ignores error if the type is unexpected |
34+
| [] | Array construction |
35+
| {} | Object construction |
36+
| + | Concatenate or Add |
37+
| - | Difference of sets or Substract |
38+
| length | Size of selected element |
39+
| | | Pipes are used to chain commands in a similar fashion than bash|
40+
41+
42+
## Dealing with json objects
43+
44+
| Description | Command |
45+
| ------ | :--------- |
46+
| Display all keys | `jq 'keys'` |
47+
| Adds + 1 to all items | `jq 'map_values(.+1)'` |
48+
| Delete a key| `jq 'del(.foo)'` |
49+
| Convert an object to array | `to_entries | map([.key, .value])` |
50+
51+
## Dealing with fields
52+
53+
| Description | Command |
54+
| ------ | :--------- |
55+
| Concatenate two fields| `fieldNew=.field1+' '+.field2` |
56+
57+
58+
## Dealing with json arrays
59+
60+
### Slicing and Filtering
61+
62+
| Description | Command |
63+
| ------ | :--------- |
64+
| All | `jq .[]` |
65+
| First | `jq '.[0]'` |
66+
| Range | `jq '.[2:4]'` |
67+
| First 3 | `jq '.[:3]'` |
68+
| Last 2 | `jq '.[-2:]'` |
69+
| Before Last | `jq '.[-2]'`|
70+
| Select array of int by value | `jq 'map(select(. >= 2))'` |
71+
| Select array of objects by value| `jq '.[] | select(.id == "second")'` |
72+
| Select by type | `jq '.[] | numbers'` with type been arrays, objects, iterables, booleans, numbers, normals, finites, strings, nulls, values, scalars |
73+
74+
### Mapping and Transforming
75+
76+
| Description | Command |
77+
| ------ | :--------- |
78+
| Add + 1 to all items | `jq 'map(.+1)'` |
79+
| Delete 2 items| `jq 'del(.[1, 2])'` |
80+
| Concatenate arrays | `jq 'add'` |
81+
| Flatten an array | `jq 'flatten'` |
82+
| Create a range of numbers | `jq '[range(2;4)]'` |
83+
| Display the type of each item| `jq 'map(type)'` |
84+
| Sort an array of basic type| `jq 'sort'` |
85+
| Sort an array of objects | `jq 'sort_by(.foo)'` |
86+
| Group by a key - opposite to flatten | `jq 'group_by(.foo)'` |
87+
| Minimun value of an array| `jq 'min'` .See also min, max, min_by(path_exp), max_by(path_exp) |
88+
| Remove duplicates| `jq 'unique'` or `jq 'unique_by(.foo)'` or `jq 'unique_by(length)'` |
89+
| Reverse an array | `jq 'reverse'` |

0 commit comments

Comments
 (0)