jq Command
JSON Query command for filtering data out of a JSON Object, file, or URL. jq is a query language built specifically for interacting with JSON type data. More information can be found at https://jmespath.org/ as well as an online version to test your query Pass or pipe the text to process or a filename

Using jq with a file

1
# basic.json
2
"a": {
3
"b": {
4
"c": {
5
"d": "value"
6
}
7
}
8
},
9
"dan": [1,2,3,4,5,6,7,8],
10
"ban": "bar",
11
"cat": "baz"
12
}
Copied!
1
> jq "basic.json" "a"
2
=> "a": {
3
"b": {
4
"c": {
5
"d": "value"
6
}
7
}
8
}
Copied!
1
> jq "basic.json" "a.b.c.d"
2
=> value
Copied!
1
> jq "basic.json" "dan"
2
=> [
3
1,
4
2,
5
3,
6
4,
7
5,
8
6,
9
7,
10
8
11
]
Copied!
1
> jq "basic.json" "dan[1]"
2
=> 2
Copied!

Using jq with a URL

1
> jq "https://official-joke-api.appspot.com/jokes/ten" [].join('.....',[setup,punchline])
2
=> [
3
"Lady: How do I spread love in this cruel world?.....Random Dude: [...\ud83d\udc98]",
4
"Why are skeletons so calm?.....Because nothing gets under their skin.",
5
"How come a man driving a train got struck by lightning?.....He was a good conductor.",
6
"What do you call a pig with three eyes?.....Piiig",
7
"How do you steal a coat?.....You jacket.",
8
"Did you hear about the submarine industry?.....It really took a dive...",
9
"What does a pirate pay for his corn?.....A buccaneer!",
10
"A programmer puts two glasses on his bedside table before going to sleep......A full one, in case he gets thirsty, and an empty one, in case he doesn’t.",
11
"How did the hipster burn the roof of his mouth?.....He ate the pizza before it was cool.",
12
"Did you hear the news?.....FedEx and UPS are merging. They’re going to go by the name Fed-Up from now on."
13
]
Copied!

Using jq with inline json

1
> jq '{"a": {"b": {"c": {"d": "value"}}}}' a.b.c.d
2
=> value
Copied!

Special Keys / Expressions

@ - Current Node (eg. current number/string/array/object) used to evaluate or check value
& - Expression (Function or Keyname) (eg. &to_number() or &keyname)
! - NOT Expression
&& - AND expression
|| - OR expression
{'ab':true} - Literal Expressions (this will be converted to json)
'foo' - Raw String Literals not evaluated (Single Quotes)

Available Functions

Generic Functions

length, reverse, type, not_null
1
CommandBox> jq [1,2,3,4,5,6,7,8,9] length(@)
2
=> 9
Copied!

Conversion Functions

to_list, to_array, to_string, to_number
1
CommandBox> jq [1,2,-3,4,-5,6,-7,-8,9] [].to_string(@)
2
> [
3
"1",
4
"2",
5
"-3",
6
"4",
7
"-5",
8
"6",
9
"-7",
10
"-8",
11
"9"
12
]
Copied!

String / Number Functions

abs, ceil, floor
1
CommandBox> jq [1,2,-3,4,-5,6,-7,-8,9] [].abs(@)
2
=> [
3
1,
4
2,
5
3,
6
4,
7
5,
8
6,
9
7,
10
8,
11
9
12
]
Copied!

Boolean Checks

ends_with, starts_with, contains
1
CommandBox> jq [1,2,-3,4,-5,6,-7,-8,9] [?contains('-8',@)]
2
=> [
3
-8
4
]
Copied!

All functions can be used in other functions with the "&" operator.

A common example would be getting a person with the highest or lowest networth max_by(people, &abs(net_worth))

Array Functions

avg: ( ARR ) - convert array of number to average (ex. [1,2,3] -> 2)
first: ( ARR/STR ) - convience method to get the first item
group_by: ( ARR ) - Splits a collection into sets
join: ( ARR, STR ) - concatenate an array of strings/numbers with a provided delimiter to a string
last: ( ARR/STR ) - convience method to get the last item
matches: ( STR/ARR, searchTerm ) - regex match string
min: ( ARR ) - get the minimum string/number/dates of an array (ex. [1,2,3] -> 1)
max: ( ARR ) - get the maximum string/number/dates of an array (ex. [1,2,3] -> 3)
reverse: ( STR/ARR ) - returns a reversal of a string or array
sum: ( ARR ) - convert array of number to sum (ex. [1,2,3] -> 6)
sort: ( STR_ARR/NUM_ARR ) - sorts an array of strings/numbers/dates
split: ( ARR/STR, STR ) - splits strings into arrays
unique/uniq: ( ARR ) - remove duplicates
1
CommandBox> jq [1,2,-3,4,-5,6,-7,-8,9] avg(@)
2
-0.111111111111
Copied!

Struct or Array of Structs functions

defaults: ( OBJ/ARR, OBJ ) - sets default values if missing on 1 or more structs
key_contains ( OBJ, &KeyName ) - boolean check if struct contains key name
from_entries ( OBJ/ARR ) - converts a {type:orange} -> {key: type, value:orange}
keys: ( OBJ/ARR ) - returns an array of keys
max_by: ( ARR,Function/Key ) - same as min but targets a key inside the array and returns a single struct
merge: ( OBJ/ARR, ...) - Merges objects into one single object with overwrite
min_by: ( ARR,Function/Key ) - same as max but targets a key inside the array and returns a single struct
omit ( OBJ/ARR, STR/ARR ) - loops over 1+ struct and excludes keys provided to_pairs: ( OBJ/ARR )- converts a {type:orange} -> [[type, orange]]
pluck ( OBJ/ARR, STR/ARR ) - loops over 1+ struct and only includes keys provided
sort_by: ( ARR, Function/Key ) - same as sort but targets a key inside the array and returns the entire array
to_entries ( OBJ/ARR ) - converts a {type:orange} -> {key: type, value:orange}
values: ( OBJ/ARR ) - returns an array of values
map: ( Function/Key, ARR ) -
1
# jsonfile.json
2
3
CommandBox> jq box.json pluck(@,'name') # only show 'name' key
4
5
CommandBox> jq box.json omit(@,'name,version') # show all keys except 'name & version' keys
6
7
CommandBox> jq box.json keys(@) # list an array of all keys in a struct
8
9
CommandBox> jq box.json values(@) # list an array of all values in a struct
10
11
CommandBox> jq box.json to_entries(@)
12
=> [
13
{
14
"key":"author",
15
"value":"Scott Steinbeck"
16
},
17
{
18
"key":"bugs",
19
"value":""
20
},
21
{
22
"key":"changelog",
23
"value":""
24
}...
25
26
27
Commandbox> jq box.json key_contains(@,'on')
28
=> {
29
"shortDescription":"",
30
"instructions":"",
31
"version":"0.0.0",
32
"location":"ForgeboxStorage",
33
"documentation":"",
34
"contributors":[],
35
"description":""
36
}
37
38
# filter the server list and then group by the value of the status key
39
Commandbox> server list --json | jq "group_by([].{name: name, status: status},'status')"
40
41
# Can also be written with pipes
42
# Step 1. filter server list array to just name and status keys
43
# JQ Pipe to next function
44
# Step 2. group by @ (each item) where the value of 'status' is the same
45
Commandbox> server list --json | jq "[].{name: name, status: status} | group_by(@,'status')"
46
47
=> {
48
"running":[
49
{
50
"status":"running",
51
"name":"Server 1"
52
}
53
],
54
"stopped":[
55
{
56
"status":"stopped",
57
"name":"Server 2"
58
},...
59
60
CommandBox> jq jsonfile.json sort_by(@,&Size)
61
=> [
62
{
63
"logdir":"logs/bb",
64
"Size":303
65
},
66
{
67
"logdir":"logs/aa",
68
"Size":308
69
}
70
]
Copied!
Last modified 22d ago