-
Notifications
You must be signed in to change notification settings - Fork 1
/
jsonpointer.jq
47 lines (44 loc) · 979 Bytes
/
jsonpointer.jq
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
module {
"name": "jsonpointer",
"description": "jq module implementing JSON Pointer (RFC 6901)",
"homepage": "https://github.com/nichtich/jq-jsonpointer#readme",
"license": "MIT",
"author": "Jakob Voß",
"version": "1.0.0",
"jq": "1.5",
"repository": {
"type": "git",
"url": "https://github.com/nichtich/jq-jsonpointer.git"
}
};
def pointer_tokens:
. as $pointer |
if $pointer == "" then
[]
else
$pointer | split("/") |
if .[0] == "" then
.[1:] | map(gsub("~1";"/";"g")|gsub("~0";"~";"g"))
else
error("Invalid JSON Pointer: \($pointer)")
end
end
;
def pointer_get(tokens):
reduce (tokens | .[]) as $token (
.;
if type == "object" then
.[$token]
elif type != "array" then
empty
elif $token|test("^0$|^[1-9][0-9]*$") then
.[$token|tonumber]
else
empty
end
)
;
def pointer(json_pointer):
(json_pointer | pointer_tokens) as $tokens |
pointer_get($tokens)
;