-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharticle-json.html
More file actions
executable file
·431 lines (394 loc) · 18.3 KB
/
article-json.html
File metadata and controls
executable file
·431 lines (394 loc) · 18.3 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
<!DOCTYPE html>
<html lang="en">
<head>
<!--__RIM_REDIRECT__-->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Quicksand:wght@300..700&display=swap" rel="stylesheet">
<title>Fast JSON parser with little coding</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="HandheldFriendly" content="True"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<link rel="canonical" href="https://rimstone-lang.com/article-json.html" />
<style>
body, html {
font-family: Quicksand,monospace,Helvetica, Arial, sans-serif;
line-height: 150%;
font-size:16px;
}
body {
padding-left:1vw;
padding-right:1vw;
}
.ncode {
letter-spacing: 0px;
font-family: monospace;
font-size:83%;
display:inline-block;
max-width:99%;
min-width:90%;
margin:0;
padding:0;
padding-left:5px;
padding-top:3px;
padding-bottom:3px;
margin-bottom:8px;
margin-top:8px;
border: 2px solid #d6d6d6;
background-color:#f5f7f4;
white-space:nowrap;
}
.shcode {
letter-spacing: 0px;
font-family: monospace;
font-size:83%;
display:inline-block;
max-width:99%;
min-width:90%;
margin:0;
padding:0;
padding-left:5px;
padding-top:3px;
padding-bottom:3px;
margin-bottom:8px;
margin-top:8px;
border: 2px solid #d6d6d6;
background-color:#f5f7f4;
white-space:nowrap;
}
.sqlcode {
letter-spacing: 0px;
font-family: monospace;
font-size:83%;
display:inline-block;
max-width:99%;
min-width:90%;
margin:0;
padding:0;
padding-left:5px;
padding-top:3px;
padding-bottom:3px;
margin-bottom:8px;
margin-top:8px;
border: 2px solid #d6d6d6;
background-color:#f5f7f4;
white-space:nowrap;
}
.htmlcode {
letter-spacing: 0px;
font-family: monospace;
font-size:83%;
display:inline-block;
max-width:99%;
min-width:90%;
margin:0;
padding:0;
padding-left:5px;
padding-top:3px;
padding-bottom:3px;
margin-bottom:8px;
margin-top:8px;
border: 2px solid #d6d6d6;
background-color:#f5f7f4;
white-space:nowrap;
}
.code {
letter-spacing: 0px;
font-family: monospace;
font-size:83%;
display:inline-block;
max-width:99%;
min-width:90%;
margin:0;
padding:0;
padding-left:5px;
padding-top:3px;
padding-bottom:3px;
margin-bottom:8px;
margin-top:8px;
border: 2px solid #d6d6d6;
background-color:#f5f7f4;
white-space:nowrap;
}
/*Just like h1 but for pdf conversion it would be indented this way it's not*/
.vhub {
display: block;
font-size: 1.6em;
margin-top: 0.63em;
margin-bottom: 0.63em;
margin-left: 0;
margin-right: 0;
font-weight: bold;
}
/*Just like h2 but for pdf conversion it would be indented this way it's not*/
.vsub {
display: block;
font-size: 1.25em;
margin-top: 0.53em;
margin-bottom: 0.53em;
margin-left: 0;
margin-right: 0;
font-weight: bold;
}
ul {
margin-left: 0.75vw;
padding-left: 0;
}
li {
margin-left: 0.75vw;
padding-left: 0;
}
/* this must be last, as it overrides previous settings, for mobile */
@media (hover: none) {
a {
display: inline-block;
padding-top: 3px;
padding-bottom: 2px;
}
body {
padding-left:2vw;
padding-right:2vw;
letter-spacing: 1px;
}
}
/*The following is for code snippets that are highlighted by 2html vim*/
pre { overflow-x: scroll; margin:0; padding:0; font-family:monospace; }
.Identifier { color: #008b8b; }
.Statement { color: #af5f00; }
.PreProc { color: #5fd7ff; }
.Type { color: #005f00; }
.Comment { color: blue ; }
.Constant { color: #ff00ff; }
/*end of highlighted snippets*/
a {
text-decoration:none;
padding-bottom: 0px;
color:inherit;
border-bottom: 2px solid #6cb8f0;
}
a:hover {
text-decoration: none;
color:black;
border-bottom: 1px solid red;
}
/*do not underline links nor should they be active*/
pre a {
text-decoration:none;
color:black;
border-bottom: none;
pointer-events: none;
cursor: default;
}
.golfSnippet {display:none;}
ul {
list-style-type:square;
list-style-position: outside;
}
</style>
</head>
<body>
<div id="google_translate_element" style='float:right'></div>
<script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
<script>
function rim_copy(gt, eid, gc) {
gt.textContent = eid.textContent;
gt.select();
document.execCommand("copy");
gc.style.visibility="visible"
setTimeout(()=>{ gc.style.visibility="hidden"; }, 1000);
}
function googleTranslateElementInit() {
new google.translate.TranslateElement({pageLanguage: 'en'}, 'google_translate_element');
}
</script>
<!--RIMMENU13-->
<!--RIMENDMENU13-->
<!--BEGVDOC90-->
<div class='vhub' style='margin-top:10px;margin-right:20px;text-align:left;background-color:white;'><a href='https://rimstone-lang.com' style='border-bottom:0px'><img src='https://rimstone-lang.com/rimstone.png' style='width:180px;height:auto'/></a></div><div class='vhub' style='margin-top:10px;'>Fast JSON parser with little coding</div><hr/><br/>
RimStone's JSON parser produces an array of name/value pairs. A name is a path to value, for instance "country"."state"."name", and the value is simply the data associated with it, for instance "Idaho". You can control if the name contains array indexes or not, for instance if there are multiple States in the document, you might have names like "country"."state"[0]."name" with [..] designating an array element.<br/>
<br/>
You can iterate through this array and get names of JSON elements, examine if they are of interest to you, and if so, get the values. This typical scenario is how RimStone's parser is built, since it uses a "lazy" approach, where values are not allocated until needed, speeding up parsing. That is the case in this example. The JSON document below is examined and only the names of the cities are extracted.<br/>
<br/>
You can also store JSON elements into trees or hashes for future fast retrieval, or store them into a database, etc.<br/>
<br/>
To get started, create a directory for this example and position in it:<br/>
<div class="shcode" style='position:relative;padding-right:16px;'>
<pre id='code_207' class=notranslate>
<span class="Statement">mkdir</span> <span class="Special">-p</span> json
<span class="Statement">cd</span> json</pre>
<span id=rimstone_copied_207 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_207' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_207, code_207, rimstone_copied_207)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
JSON will be in file "countries.json" - we will get the names of the cities from it:<br/>
<div class='ncode' style='position:relative;padding-right:16px;'>
<pre id='code_205' class=notranslate>
cat << 'EOF' > countries.json
{ "country": [
{
"name": "USA",
"state": [
{
"name": "Arizona",
"city": [
{
"name" : "Phoenix",
"population": 5000000
} ,
{
"name" : "Tuscon",
"population": 1000000
}
]
} ,
{
"name": "California",
"city": [
{
"name" : "Los Angeles",
"population": 19000000
},
{
"name" : "Irvine"
}
]
}
]
} ,
{
"name": "Mexico",
"state": [
{
"name": "Veracruz",
"city": [
{
"name" : "Xalapa-Enr\u00edquez",
"population": 8000000
},
{
"name" : "C\u00F3rdoba",
"population": 220000
}
]
} ,
{
"name": "Sinaloa",
"city": [
{
"name" : "Culiac\u00E1n Rosales",
"population": 3000000
}
]
}
]
}
]
}
EOF</pre>
<span id=rimstone_copied_205 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_205' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_205, code_205, rimstone_copied_205)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
What follows is the code to parse JSON. We open a JSON file, process the document, check for errors, and then read elements one by one. We look for a key "country"."state"."city"."name" because those contains city names. Note use "no-enum" clause in <a href='https://rimstone-lang.com/json-doc.html'>json-doc</a> (which is the RimStone's JSON parser), so that element designations aren't showing (meaning we don't have [0], [1] etc. for arrays). <br/>
<br/>
Thi code will be in "parse-json.rim":<br/>
<div class='code' style='position:relative;padding-right:16px;'>
<pre id='code_206' class=notranslate>
cat << '<span class="Constant">EOF</span>' > parse-json.rim
<span class="Statement">begin-handler</span> /parse-json<span class="Identifier"> public</span>
<span class="Comment">// Read the JSON file</span>
<span class="Statement"> read-file</span> <span class="Constant">"countries.json"</span><span class="Type"> to</span> countries<span class="Type"> status</span> st
<span class="Statement"> if-true</span> st<span class="Identifier"> lesser-equal</span> <span class="Constant">0</span>
<span class="Statement"> @</span>Cannot read file or file empty
<span class="Statement"> exit-handler</span> -<span class="Constant">1</span>
<span class="Statement"> end-if</span>
<span class="Comment">// Parse JSON</span>
<span class="Statement"> json-doc</span> countries<span class="Identifier"> no-enum</span><span class="Type"> status</span> st<span class="Type"> error-text</span> et<span class="Type"> error-position</span> ep<span class="Type"> to</span> json
<span class="Comment">// Check for errors in JSON document</span>
<span class="Statement"> if-true</span> st<span class="Identifier"> not-equal</span> RIM_OKAY
<span class="Statement"> @</span>Error [<span class="Statement"><<print-out</span> et<span class="Statement">>></span>] at [<span class="Statement"><<print-out</span> ep<span class="Statement">>></span>]
<span class="Statement"> exit-handler</span> -<span class="Constant">2</span>
<span class="Statement"> end-if</span>
<span class="Comment">// This is the JSON element we're looking for</span>
<span class="Statement"> set-string</span> city_name<span class="Identifier"> unquoted</span> =<span class="Constant">"country"</span>.<span class="Constant">"state"</span>.<span class="Constant">"city"</span>.<span class="Constant">"name"</span>
<span class="Comment">// Read elements one by one - note you can then store them in a tree or hash for future fast searches</span>
<span class="Statement"> start-loop</span>
<span class="Comment">// Read just a key</span>
<span class="Statement"> read-json</span> json<span class="Type"> key</span> k<span class="Type"> type</span> t
<span class="Comment">// Exit if end of document</span>
<span class="Statement"> if-true</span> t<span class="Identifier"> equal</span> RIM_JSON_TYPE_NONE
<span class="Statement"> break-loop</span>
<span class="Statement"> end-if</span>
<span class="Comment">// If matches key we're looking for, get the value, and output it</span>
<span class="Statement"> if-true</span> city_name<span class="Identifier"> equal</span> k
<span class="Statement"> read-json</span> json<span class="Type"> value</span> v
<span class="Statement"> @</span>Value [<span class="Statement"><<print-out</span> v<span class="Statement">>></span>]
<span class="Statement"> @</span>--------
<span class="Statement"> end-if</span>
<span class="Comment">// Move on to the next JSON element</span>
<span class="Statement"> read-json</span> json<span class="Identifier"> next</span>
<span class="Statement"> end-loop</span>
<span class="Comment">// Optionally delete JSON object, or it will be automatically deleted</span>
<span class="Statement"> json-doc</span><span class="Identifier"> delete</span> json
<span class="Statement">end-handler</span>
<span class="Constant">EOF</span></pre>
<span id=rimstone_copied_206 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_206' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_206, code_206, rimstone_copied_206)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
Create "json" application ("-k") and compile it ("-q"):<br/>
<div class="shcode" style='position:relative;padding-right:16px;'>
<pre id='code_209' class=notranslate>
rim <span class="Special">-k</span> json <span class="Special">-q</span></pre>
<span id=rimstone_copied_209 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_209' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_209, code_209, rimstone_copied_209)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
Copy the above JSON file into application home directory so we can use it without a path (since this directory is your program's default directory) - you can, of course, also specify any absolute or relative path in RimStone code above:<br/>
<div class="shcode" style='position:relative;padding-right:16px;'>
<pre id='code_210' class=notranslate>
<span class="Statement">cp</span> countries.json ~/.rimstone/apps/json/app</pre>
<span id=rimstone_copied_210 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_210' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_210, code_210, rimstone_copied_210)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
Run it:<br/>
<div class="shcode" style='position:relative;padding-right:16px;'>
<pre id='code_211' class=notranslate>
rim <span class="Special">-r</span> <span class="Special">--req</span><span class="Statement">=</span><span class="Statement">"</span><span class="Constant">/parse-json</span><span class="Statement">"</span> <span class="Special">--exec</span></pre>
<span id=rimstone_copied_211 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_211' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_211, code_211, rimstone_copied_211)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
The result is:<br/>
<div class='ncode' style='position:relative;padding-right:16px;'>
<pre id='code_208' class=notranslate>
Content-Type: text/html;charset=utf-8
Cache-Control: max-age=0, no-cache
Pragma: no-cache
Status: 200 OK
Value [Phoenix]
--------
Value [Tuscon]
--------
Value [Los Angeles]
--------
Value [Irvine]
--------
Value [Xalapa-Enríquez]
--------
Value [Córdoba]
--------
Value [Culiacán Rosales]</pre>
<span id=rimstone_copied_208 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_208' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_208, code_208, rimstone_copied_208)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
RimStone programs work exactly the same when you run them from command line as they do as web services or applications, hence you get the HTTP header in the output. You can avoid it by using <a href='https://rimstone-lang.com/silent-header.html'>silent-header</a> in your RimStone code, or by using "--silent-header" option in the above <a href='https://rimstone-lang.com/rim.html'>rim</a> execution call. <br/>
<br/>
You can also omit "--exec" option in the above rim execution call, which will display environment variable settings and the path to your executable. Then you can run those directly. This is useful if you're running a RimStone command line program in batch mode from command line repeatedly, as it's faster than using <a href='https://rimstone-lang.com/rim.html'>rim</a>.<br/>
<!--ENDVDOC90-->
<br/><div style='width:100%;clear:both;'>
<hr/>
<!--RIMFOOT77--><span style='font-size:80%'><a href="https://rimstone-lang.com/copyright.html">Copyright</a> (c) 2019-2025 Gliim LLC. All contents on this web site is "AS IS" without warranties or guarantees of any kind.</span>
</div><br/></body></html>