-
Notifications
You must be signed in to change notification settings - Fork 2
/
json2var.awk
141 lines (133 loc) · 4.59 KB
/
json2var.awk
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# =====================================================================================================
# JSON parse function. Returns a list of values parsed from json data.
# example: jsonout = json2var(jsonin)
# Returns a string containing values separated by "\n".
# See the section marked "<--" in parse_value() to customize for your application.
#
# Credits: by User:Green Cardamom at en.wikipedia.org
# JSON parser derived from JSON.awk
# https://github.com/step-/JSON.awk.git
# MIT license. May 2015
# =====================================================================================================
function json2var(jsonin) {
TOKEN=""
delete TOKENS
NTOKENS=ITOKENS=0
delete JPATHS
NJPATHS=0
VALUE=""
tokenize(jsonin)
if ( parse() == 0 ) {
return join(JPATHS,1,NJPATHS, "\n")
}
}
function parse_value(a1, a2, jpath,ret,x) {
jpath=(a1!="" ? a1 "," : "") a2 # "${1:+$1,}$2"
if (TOKEN == "{") {
if (parse_object(jpath)) {
return 7
}
} else if (TOKEN == "[") {
if (ret = parse_array(jpath)) {
return ret
}
} else if (TOKEN ~ /^(|[^0-9])$/) {
# At this point, the only valid single-character tokens are digits.
return 9
} else {
VALUE=TOKEN
}
if (! (1 == BRIEF && ("" == jpath || "" == VALUE))) {
# This will print the full JSON data to help in building custom filter
# x = sprintf("[%s]\t%s", jpath, VALUE)
# print x
if ( a2 == "\"*\"" || a2 == "\"title\"" ) { # <-- Custom filter for MediaWiki API backlinks. Add custom filters here.
x = substr(VALUE, 2, length(VALUE) - 2)
NJPATHS++
JPATHS[NJPATHS] = x
}
}
return 0
}
function get_token() {
TOKEN = TOKENS[++ITOKENS] # for internal tokenize()
return ITOKENS < NTOKENS
}
function parse_array(a1, idx,ary,ret) {
idx=0
ary=""
get_token()
if (TOKEN != "]") {
while (1) {
if (ret = parse_value(a1, idx)) {
return ret
}
idx=idx+1
ary=ary VALUE
get_token()
if (TOKEN == "]") {
break
} else if (TOKEN == ",") {
ary = ary ","
} else {
return 2
}
get_token()
}
}
VALUE=""
return 0
}
function parse_object(a1, key,obj) {
obj=""
get_token()
if (TOKEN != "}") {
while (1) {
if (TOKEN ~ /^".*"$/) {
key=TOKEN
} else {
return 3
}
get_token()
if (TOKEN != ":") {
return 4
}
get_token()
if (parse_value(a1, key)) {
return 5
}
obj=obj key ":" VALUE
get_token()
if (TOKEN == "}") {
break
} else if (TOKEN == ",") {
obj=obj ","
} else {
return 6
}
get_token()
}
}
VALUE=""
return 0
}
function parse( ret) {
get_token()
if (ret = parse_value()) {
return ret
}
if (get_token()) {
return 11
}
return 0
}
function tokenize(a1, myspace) {
# POSIX character classes (gawk)
# Replaced regex constant for string constant, see https://github.com/step-/JSON.awk/issues/1
myspace="[[:space:]]+"
gsub(/\"[^[:cntrl:]\"\\]*((\\[^u[:cntrl:]]|\\u[0-9a-fA-F]{4})[^[:cntrl:]\"\\]*)*\"|-?(0|[1-9][0-9]*)([.][0-9]*)?([eE][+-]?[0-9]*)?|null|false|true|[[:space:]]+|./, "\n&", a1)
gsub("\n" myspace, "\n", a1)
sub(/^\n/, "", a1)
ITOKENS=0
return NTOKENS = split(a1, TOKENS, /\n/)
}