Skip to content

Commit a084047

Browse files
author
Jonathan Cook
committed
BAEL-3372 - Guide to Linux jq command for JSON formatting
1 parent c1829f6 commit a084047

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"fruit":{"name":"apple","color":"green","price":1.20}}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[
2+
{
3+
"name": "apple",
4+
"color": "green",
5+
"price": 1.2
6+
},
7+
{
8+
"name": "banana",
9+
"color": "yellow",
10+
"price": 0.5
11+
},
12+
{
13+
"name": "kiwi",
14+
"color": "green",
15+
"price": 1.25
16+
}
17+
]

linux-bash/json/src/main/bash/jq.sh

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/bin/bash
2+
3+
#3.1. Beautify JSON
4+
echo '{"fruit":{"name":"apple","color":"green","price":1.20}}' | jq '.'
5+
jq '.' fruit.json
6+
curl http://api.open-notify.org/iss-now.json | jq '.'
7+
8+
#3.2. Accessing Properties
9+
jq '.fruit' fruit.json
10+
jq '.fruit.color' fruit.json
11+
jq '.fruit.color,.fruit.price' fruit.json
12+
echo '{ "with space": "hello" }' | jq '."with space"'
13+
14+
#4.1. Iteration
15+
echo '["x","y","z"]' | jq '.[]'
16+
jq '.[] | .name' fruits.json
17+
jq '.[].name' fruits.json
18+
19+
#4.2. Accessing By Index
20+
jq '.[1].price' fruits.json
21+
22+
#4.3. Slicing
23+
echo '[1,2,3,4,5,6,7,8,9,10]' | jq '.[6:9]'
24+
echo '[1,2,3,4,5,6,7,8,9,10]' | jq '.[:6]' | jq '.[-2:]'
25+
26+
#5.1. Getting Keys
27+
jq '.fruit | keys' fruit.json
28+
29+
#5.2. Returning the Length
30+
jq '.fruit | length' fruit.json
31+
jq '.fruit.name | length' fruit.json
32+
33+
#5.3. Mapping Values
34+
jq 'map(has("name"))' fruits.json
35+
jq 'map(.price+2)' fruits.json
36+
37+
#5.4. Min and Max
38+
jq '[.[].price] | min' fruits.json
39+
jq '[.[].price] | max' fruits.json
40+
41+
#5.5. Selecting Values
42+
jq '.[] | select(.price>0.5)' fruits.json
43+
jq '.[] | select(.color=="yellow")' fruits.json
44+
jq '.[] | select(.color=="yellow" and .price>=0.5)' fruits.json
45+
46+
#5.6. Support For RegEx
47+
jq '.[] | select(.name|test("^a.")) | .price' fruits.json
48+
49+
#5.7. Find Unique Values
50+
jq 'map(.color) | unique' fruits.json
51+
52+
#5.8. Deleting Keys From JSON
53+
jq 'del(.fruit.name)' fruit.json
54+
55+
# 6. Transforming
56+
jq '.query.pages | [.[] | map(.) | .[] | {page_title: .title, page_description: .extract}]' wikipedia.json
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"query": {
3+
"pages": [
4+
{
5+
"21721040": {
6+
"pageid": 21721040,
7+
"ns": 0,
8+
"title": "Stack Overflow",
9+
"extract": "Some interesting text about Stack Overflow"
10+
}
11+
},
12+
{
13+
"21721041": {
14+
"pageid": 21721041,
15+
"ns": 0,
16+
"title": "Baeldung",
17+
"extract": "A great place to learn about Java"
18+
}
19+
}
20+
]
21+
}
22+
}

0 commit comments

Comments
 (0)