File: jo.1

package info (click to toggle)
jo 1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 360 kB
  • ctags: 154
  • sloc: ansic: 1,680; sh: 485; makefile: 39; exp: 14
file content (371 lines) | stat: -rw-r--r-- 7,401 bytes parent folder | download
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
.\"t
.\" Automatically generated by Pandoc 1.19.2.1
.\"
.TH "JO" "1" "" "User Manuals" ""
.hy
.SH NAME
.PP
jo \- JSON output from a shell
.SH SYNOPSIS
.PP
jo [\-p] [\-a] [\-B] [\-v] [\-V] [\-\-] [ [\-s|\-n|\-b] word ...]
.SH DESCRIPTION
.PP
\f[I]jo\f[] creates a JSON string on \f[I]stdout\f[] from _word_s given
it as arguments or read from \f[I]stdin\f[].
Without option \f[C]\-a\f[] it generates an object whereby each
\f[I]word\f[] is a \f[C]key=value\f[] (or \f[C]key\@value\f[]) pair with
\f[I]key\f[] being the JSON object element and \f[I]value\f[] its value.
\f[I]jo\f[] attempts to guess the type of \f[I]value\f[] in order to
create number (using \f[I]strtod(3)\f[]), string, or null values in
JSON.
.PP
\f[I]jo\f[] treats \f[C]key\@value\f[] specifically as boolean JSON
elements: if the value begins with \f[C]T\f[], \f[C]t\f[], or the
numeric value is greater than zero, the result is \f[C]true\f[], else
\f[C]false\f[].
A missing or empty value behind the colon results in a \f[C]null\f[]
JSON element.
.PP
\f[I]jo\f[] creates an array instead of an object when \f[C]\-a\f[] is
specified.
.PP
When the \f[C]:=\f[] operator is used in a \f[I]word\f[], the name to
the right of \f[C]:=\f[] is a file containing JSON which is parsed and
assigned to the key left of the operator.
.SH TYPE COERCION
.PP
\f[I]jo\f[]\[aq]s type guesses can be overridden on a per\-word basis by
prefixing \f[I]word\f[] with \f[C]\-s\f[] for \f[I]string\f[],
\f[C]\-n\f[] for \f[I]number\f[], or \f[C]\-b\f[] for \f[I]boolean\f[].
The list of _word_s \f[I]must\f[] be prefixed with \f[C]\-\-\f[], to
indicate to \f[I]jo\f[] that there are no more global options.
.PP
Type coercion works as follows:
.PP
.TS
tab(@);
l l l l l.
T{
word
T}@T{
\-s
T}@T{
\-n
T}@T{
\-b
T}@T{
default
T}
_
T{
a=
T}@T{
"a":""
T}@T{
"a":0
T}@T{
"a":false
T}@T{
"a":null
T}
T{
a=string
T}@T{
"a":"string"
T}@T{
"a":6
T}@T{
"a":true
T}@T{
"a":"string"
T}
T{
a="quoted"
T}@T{
"a":""quoted""
T}@T{
"a":8
T}@T{
"a":true
T}@T{
"a":""quoted""
T}
T{
a=12345
T}@T{
"a":"12345"
T}@T{
"a":12345
T}@T{
"a":true
T}@T{
"a":12345
T}
T{
a=true
T}@T{
"a":"true"
T}@T{
"a":1
T}@T{
"a":true
T}@T{
"a":true
T}
T{
a=false
T}@T{
"a":"false"
T}@T{
"a":0
T}@T{
"a":false
T}@T{
"a":false
T}
T{
a=null
T}@T{
"a":""
T}@T{
"a":0
T}@T{
"a":false
T}@T{
"a":null
T}
.TE
.PP
Coercing a non\-number string to number outputs the \f[I]length\f[] of
the string.
.PP
Coercing a non\-boolean string to boolean outputs \f[C]false\f[] if the
string is empty, \f[C]true\f[] otherwise.
.PP
Type coercion only applies to \f[C]key=value\f[] words, and individual
words in a \f[C]\-a\f[] array.
Coercing other words has no effect.
.SH EXAMPLES
.PP
Create an object.
Note how the incorrectly\-formatted float value becomes a string:
.IP
.nf
\f[C]
$\ jo\ tst=1457081292\ lat=12.3456\ cc=FR\ badfloat=3.14159.26\ name="JP\ Mens"\ nada=\ coffee\@T
{"tst":1457081292,"lat":12.3456,"cc":"FR","badfloat":"3.14159.26","name":"JP\ Mens","nada":null,"coffee":true}
\f[]
.fi
.PP
Pretty\-print an array with a list of files in the current directory:
.IP
.nf
\f[C]
$\ jo\ \-p\ \-a\ *
[
\ "Makefile",
\ "README.md",
\ "jo.1",
\ "jo.c",
\ "jo.pandoc",
\ "json.c",
\ "json.h"
]
\f[]
.fi
.PP
Create objects within objects; this works because if the first character
of value is an open brace or a bracket we attempt to decode the
remainder as JSON.
Beware spaces in strings ...
.IP
.nf
\f[C]
$\ jo\ \-p\ name=JP\ object=$(jo\ fruit=Orange\ hungry\@0\ point=$(jo\ x=10\ y=20\ list=$(jo\ \-a\ 1\ 2\ 3\ 4\ 5))\ number=17)\ sunday\@0
{
\ "name":\ "JP",
\ "object":\ {
\ \ "fruit":\ "Orange",
\ \ "hungry":\ false,
\ \ "point":\ {
\ \ \ "x":\ 10,
\ \ \ "y":\ 20,
\ \ \ "list":\ [
\ \ \ \ 1,
\ \ \ \ 2,
\ \ \ \ 3,
\ \ \ \ 4,
\ \ \ \ 5
\ \ \ ]
\ \ },
\ \ "number":\ 17
\ },
\ "sunday":\ false
}
\f[]
.fi
.PP
Booleans as strings or as boolean (pay particular attention to
\f[I]switch\f[]; the \f[C]\-B\f[] option disables the default detection
of the "\f[C]true\f[]", "\f[C]false\f[]", and "\f[C]null\f[]" strings):
.IP
.nf
\f[C]
$\ jo\ switch=true\ morning\@0
{"switch":true,"morning":false}

$\ jo\ \-B\ switch=true\ morning\@0
{"switch":"true","morning":false}
\f[]
.fi
.PP
Elements (objects and arrays) can be nested.
The following example nests an array called \f[I]point\f[] and an object
named \f[I]geo\f[]:
.IP
.nf
\f[C]
$\ jo\ \-p\ name=Jane\ point[]=1\ point[]=2\ geo[lat]=10\ geo[lon]=20
{
\ \ \ "name":\ "Jane",
\ \ \ "point":\ [
\ \ \ \ \ \ 1,
\ \ \ \ \ \ 2
\ \ \ ],
\ \ \ "geo":\ {
\ \ \ \ \ \ "lat":\ 10,
\ \ \ \ \ \ "lon":\ 20
\ \ \ }
}
\f[]
.fi
.PP
Type coercion:
.IP
.nf
\f[C]
$\ jo\ \-p\ \-\-\ \-s\ a=true\ b=true\ \-s\ c=123\ d=123\ \-b\ e="1"\ \-b\ f="true"\ \-n\ g="This\ is\ a\ test"\ \-b\ h="This\ is\ a\ test"
{
\ \ \ "a":\ "true",
\ \ \ "b":\ true,
\ \ \ "c":\ "123",
\ \ \ "d":\ 123,
\ \ \ "e":\ true,
\ \ \ "f":\ true,
\ \ \ "g":\ 14,
\ \ \ "h":\ true
}

$\ jo\ \-a\ \-\-\ \-s\ 123\ \-n\ "This\ is\ a\ test"\ \-b\ C_Rocks\ 456
["123",14,true,456]
\f[]
.fi
.PP
Read element values from files: a value which starts with \f[C]\@\f[] is
read in plain whereas if it begins with a \f[C]%\f[] it will be
base64\-encoded:
.IP
.nf
\f[C]
$\ jo\ program=jo\ authors=\@AUTHORS
{"program":"jo","authors":"Jan\-Piet\ Mens\ <jpmens\@gmail.com>"}

$\ jo\ filename=AUTHORS\ content=%AUTHORS
{"filename":"AUTHORS","content":"SmFuLVBpZXQgTWVucyA8anBtZW5zQGdtYWlsLmNvbT4K"}
\f[]
.fi
.PP
Read element values from a file in order to overcome ARG_MAX limits
during object assignment:
.IP
.nf
\f[C]
$\ ls\ |\ jo\ \-a\ >\ child.json
$\ jo\ files:=child.json
{"files":["AUTHORS","COPYING","ChangeLog"\ ....
\f[]
.fi
.SH OPTIONS
.PP
\f[I]jo\f[] understands the following global options.
.TP
.B \-a
Interpret the list of \f[I]words\f[] as array values and produce an
array instead of an object.
.RS
.RE
.TP
.B \-B
By default \f[I]jo\f[] interprets the strings "\f[C]true\f[]" and
"\f[C]false\f[]" as boolean elements \f[C]true\f[] and \f[C]false\f[]
respectively, and "\f[C]null\f[]" as \f[C]null\f[].
Disable with this option.
.RS
.RE
.TP
.B \-p
Pretty\-print the JSON string on output instead of the terse one\-line
output it prints by default.
.RS
.RE
.TP
.B \-v
Show version and exit.
.RS
.RE
.TP
.B \-V
Show version as a JSON object and exit.
.RS
.RE
.SH BUGS
.PP
Probably.
.PP
If a value given to \f[I]jo\f[] expands to empty in the shell, then
\f[I]jo\f[] produces a \f[C]null\f[] in object mode, and might appear to
hang in array mode; it is not hanging, rather it\[aq]s reading
\f[I]stdin\f[].
This is not a bug.
.PP
Numeric values are converted to numbers which can produce undesired
results.
If you quote a numeric value, \f[I]jo\f[] will make it a string.
Compare the following:
.IP
.nf
\f[C]
$\ jo\ a=1.0
{"a":1}
$\ jo\ a=\\"1.0\\"
{"a":"1.0"}
\f[]
.fi
.PP
Omitting a closing bracket on a nested element causes a diagnostic
message to print, but the output contains garbage anyway.
This was designed thusly.
.SH RETURN CODES
.PP
\f[I]jo\f[] exits with a code 0 on success and non\-zero on failure
after indicating what caused the failure.
.SH AVAILABILITY
.PP
<http://github.com/jpmens/jo>
.SH CREDITS
.IP \[bu] 2
This program uses \f[C]json.[ch]\f[], by Joseph A.
Adams.
.SH SEE ALSO
.IP \[bu] 2
<https://stedolan.github.io/jq/>
.IP \[bu] 2
<https://github.com/micha/jsawk>
.IP \[bu] 2
<https://github.com/jtopjian/jsed>
.IP \[bu] 2
strtod(3)
.SH AUTHOR
.PP
Jan\-Piet Mens <http://jpmens.net>