-
Notifications
You must be signed in to change notification settings - Fork 1
/
format_generic_json.sh
executable file
·67 lines (57 loc) · 1.49 KB
/
format_generic_json.sh
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
#!/usr/bin/env bash
#
# Convert json into csv
#
# Optionally strip root objects containing arrays of target objects (e.g. ignore Reservation info in "aws ec2 describe-instances" output)
#
# Converts "Tags" arrays in objects into inline fields
#
# Arguments are fields to include (and header), and number of top level objects to ignore
#
set -o nounset
set -o pipefail
set -o errexit
if [ ! "${1:-}" ]
then
echo "usage: format_generic_json.sh <comma separated header fields> [ <# of root objects to strip off> ]"
exit 2
fi
fields="$1"
if [ "${2:-}" ]
then
object_levels_to_prune="$2"
else
object_levels_to_prune=""
fi
basename="$(basename "$0")"
tempfile=$(mktemp /tmp/$basename.XXXXXX)
tempfile2=$(mktemp /tmp/$basename.XXXXXX)
if [ "$object_levels_to_prune" ]
then
for i in $(seq 1 $object_levels_to_prune)
do
cat | jq '.[][][] | select(type=="array") | .[]' > "$tempfile"
done
else
cat | jq '.[][]' > "$tempfile"
fi
# For each field, put a reference in a big string to pass to jq
#
field_references=""
for field in $(echo $fields | tr , \\n)
do
if [ "$field_references" ]
then
field_references="${field_references} + \",\" + "
fi
field_references="${field_references}(.$field | tostring)"
done
# Move fields from Tags member array into objects
#
cat "$tempfile" | jq 'if has("Tags") then ( .Tags? | from_entries) else { } end + ( . | del(.Tags?) )' > "$tempfile2"
# Display header
#
echo $fields
# Display specified fields
#
cat "$tempfile2" | jq -r '('"$field_references"')'