From a2134bf8c40027aadcfcbc1fea39cc55f673f76a Mon Sep 17 00:00:00 2001 From: Jonathan Cook Date: Sun, 3 Nov 2019 10:20:18 +0100 Subject: [PATCH] BAEL-3372 - Guide to Linux jq command for JSON formatting --- linux-bash/json/src/main/bash/fruit.json | 1 + linux-bash/json/src/main/bash/fruits.json | 17 ++++++ linux-bash/json/src/main/bash/jq.sh | 56 ++++++++++++++++++++ linux-bash/json/src/main/bash/wikipedia.json | 22 ++++++++ 4 files changed, 96 insertions(+) create mode 100644 linux-bash/json/src/main/bash/fruit.json create mode 100644 linux-bash/json/src/main/bash/fruits.json create mode 100755 linux-bash/json/src/main/bash/jq.sh create mode 100644 linux-bash/json/src/main/bash/wikipedia.json diff --git a/linux-bash/json/src/main/bash/fruit.json b/linux-bash/json/src/main/bash/fruit.json new file mode 100644 index 0000000000..392d2290ae --- /dev/null +++ b/linux-bash/json/src/main/bash/fruit.json @@ -0,0 +1 @@ +{"fruit":{"name":"apple","color":"green","price":1.20}} diff --git a/linux-bash/json/src/main/bash/fruits.json b/linux-bash/json/src/main/bash/fruits.json new file mode 100644 index 0000000000..114a9a35cc --- /dev/null +++ b/linux-bash/json/src/main/bash/fruits.json @@ -0,0 +1,17 @@ +[ + { + "name": "apple", + "color": "green", + "price": 1.2 + }, + { + "name": "banana", + "color": "yellow", + "price": 0.5 + }, + { + "name": "kiwi", + "color": "green", + "price": 1.25 + } +] diff --git a/linux-bash/json/src/main/bash/jq.sh b/linux-bash/json/src/main/bash/jq.sh new file mode 100755 index 0000000000..786be304db --- /dev/null +++ b/linux-bash/json/src/main/bash/jq.sh @@ -0,0 +1,56 @@ +#!/bin/bash + +#3.1. Beautify JSON +echo '{"fruit":{"name":"apple","color":"green","price":1.20}}' | jq '.' +jq '.' fruit.json +curl http://api.open-notify.org/iss-now.json | jq '.' + +#3.2. Accessing Properties +jq '.fruit' fruit.json +jq '.fruit.color' fruit.json +jq '.fruit.color,.fruit.price' fruit.json +echo '{ "with space": "hello" }' | jq '."with space"' + +#4.1. Iteration +echo '["x","y","z"]' | jq '.[]' +jq '.[] | .name' fruits.json +jq '.[].name' fruits.json + +#4.2. Accessing By Index +jq '.[1].price' fruits.json + +#4.3. Slicing +echo '[1,2,3,4,5,6,7,8,9,10]' | jq '.[6:9]' +echo '[1,2,3,4,5,6,7,8,9,10]' | jq '.[:6]' | jq '.[-2:]' + +#5.1. Getting Keys +jq '.fruit | keys' fruit.json + +#5.2. Returning the Length +jq '.fruit | length' fruit.json +jq '.fruit.name | length' fruit.json + +#5.3. Mapping Values +jq 'map(has("name"))' fruits.json +jq 'map(.price+2)' fruits.json + +#5.4. Min and Max +jq '[.[].price] | min' fruits.json +jq '[.[].price] | max' fruits.json + +#5.5. Selecting Values +jq '.[] | select(.price>0.5)' fruits.json +jq '.[] | select(.color=="yellow")' fruits.json +jq '.[] | select(.color=="yellow" and .price>=0.5)' fruits.json + +#5.6. Support For RegEx +jq '.[] | select(.name|test("^a.")) | .price' fruits.json + +#5.7. Find Unique Values +jq 'map(.color) | unique' fruits.json + +#5.8. Deleting Keys From JSON +jq 'del(.fruit.name)' fruit.json + +# 6. Transforming +jq '.query.pages | [.[] | map(.) | .[] | {page_title: .title, page_description: .extract}]' wikipedia.json diff --git a/linux-bash/json/src/main/bash/wikipedia.json b/linux-bash/json/src/main/bash/wikipedia.json new file mode 100644 index 0000000000..180b160bcc --- /dev/null +++ b/linux-bash/json/src/main/bash/wikipedia.json @@ -0,0 +1,22 @@ +{ + "query": { + "pages": [ + { + "21721040": { + "pageid": 21721040, + "ns": 0, + "title": "Stack Overflow", + "extract": "Some interesting text about Stack Overflow" + } + }, + { + "21721041": { + "pageid": 21721041, + "ns": 0, + "title": "Baeldung", + "extract": "A great place to learn about Java" + } + } + ] + } +}