1jello(1) Jello JSON Filter jello(1)
2
3
4
6 Jello - Filter JSON and JSON Lines data with Python syntax
7
9 Jello is similar to jq in that it processes JSON and JSON Lines data
10 except jello uses standard python dict and list syntax.
11
12 JSON or JSON Lines can be piped into jello (JSON Lines are automati‐
13 cally slurped into a list of dictionaries) and are available as the
14 variable `_`. Processed data can be output as JSON, JSON Lines, bash
15 array lines, or a grep-able schema.
16
18 cat data.json | jello [OPTIONS] [QUERY]
19
20
21 QUERY is optional and can be most any valid python code. `_` is the
22 sanitized JSON from STDIN presented as a python dict or list of dicts.
23 If QUERY is omitted then the original JSON input will simply be pretty
24 printed. You can use dot notation or traditional python bracket nota‐
25 tion to access key names.
26
27 Note: Reserved key names that cannot be accessed using dot nota‐
28 tion can be accessed via standard python dictionary notation.
29 (e.g. _.foo["get"] instead of _.foo.get)
30
31 A simple query:
32
33 $ cat data.json | jello _.foo
34
35
36 or
37
38 $ cat data.json | jello '_["foo"]'
39
40
41 Options
42 -c compact print JSON output instead of pretty printing
43
44 -C force color output even when using pipes (overrides -m and
45 the NO_COLOR env variable)
46
47 -i initialize environment with a custom config file
48
49 -l lines output (suitable for bash array assignment)
50
51 -m monochrome output
52
53 -n print selected null values
54
55 -r raw output of selected strings (no quotes)
56
57 -s print the JSON schema in grep-able format
58
59 -t print type annotations in schema view
60
61 -h help
62
63 -v version info
64
65
66 Simple Examples
67 Jello simply pretty prints the JSON if there are no options passed:
68
69 $ echo '{"foo":"bar","baz":[1,2,3]}' | jello
70 {
71 "foo": "bar",
72 "baz": [
73 1,
74 2,
75 3
76 ]
77 }
78
79
80 If you prefer compact output, use the -c option:
81
82 $ echo '{"foo":"bar","baz":[1,2,3]}' | jello -c
83 {"foo":"bar","baz":[1,2,3]}
84
85
86 Use the -l option to convert lists/arrays into lines:
87
88 $ echo '{"foo":"bar","baz":[1,2,3]}' | jello -l _.baz
89 1
90 2
91 3
92
93
94 The -l option also allows you to create JSON Lines:
95
96 $ echo '[{"foo":"bar","baz":[1,2,3]},{"fiz":"boo","buz":[4,5,6]}]' | jello -l
97 {"foo":"bar","baz":[1,2,3]}
98 {"fiz":"boo","buz":[4,5,6]}
99
100
101 You can print a grep-able schema by using the -s option:
102
103 $ echo '{"foo":"bar","baz":[1,2,3]}' | jello -s
104 _ = {};
105 _.foo = "bar";
106 _.baz = [];
107 _.baz[0] = 1;
108 _.baz[1] = 2;
109 _.baz[2] = 3;
110
111
112 Assigning Results to a Bash Array
113 Use the -l option to print JSON array output in a manner suitable to be
114 assigned to a bash array. The -r option can be used to remove quota‐
115 tion marks around strings. If you want null values to be printed as
116 null, use the -n option, otherwise they are printed as blank lines.
117
118 Bash variable:
119
120 variable=($(cat data.json | jello -rl _.foo))
121
122
123 Bash array variable:
124
125 variable=()
126 while read -r value; do
127 variable+=("$value")
128 done < <(cat data.json | jello -rl _.foo)
129
130
131 Examples:
132 Printing the Grep-able Schema
133 $ jc -a | jello -s
134 _ = {};
135 _.name = "jc";
136 _.version = "1.17.2";
137 _.description = "JSON CLI output utility";
138 _.author = "Kelly Brazil";
139 _.author_email = "kellyjonbrazil@gmail.com";
140 _.website = "https://github.com/kellyjonbrazil/jc";
141 _.copyright = "(C) 2019-2021 Kelly Brazil";
142 _.license = "MIT License";
143 _.parser_count = 80;
144 _.parsers = [];
145 ...
146
147
148 Printing the Grep-able Schema with Type Annotations
149 $ jc -a | jello -st
150 _ = {}; // (object)
151 _.name = "jc"; // (string)
152 _.version = "1.17.2"; // (string)
153 _.description = "JSON CLI output utility"; // (string)
154 _.author = "Kelly Brazil"; // (string)
155 _.author_email = "kellyjonbrazil@gmail.com"; // (string)
156 _.website = "https://github.com/kellyjonbrazil/jc"; // (string)
157 _.copyright = "(C) 2019-2021 Kelly Brazil"; // (string)
158 _.license = "MIT License"; // (string)
159 _.parser_count = 80; // (number)
160 _.parsers = []; // (array)
161 ...
162
163
164 Printing the JSON Structure
165 $ jc dig example.com | jello -st | grep '(object)\|(array)'
166 _ = []; // (array)
167 _[0] = {}; // (object)
168 _[0].flags = []; // (array)
169 _[0].opt_pseudosection = {}; // (object)
170 _[0].opt_pseudosection.edns = {}; // (object)
171 _[0].opt_pseudosection.edns.flags = []; // (array)
172 _[0].question = {}; // (object)
173 _[0].answer = []; // (array)
174 _[0].answer[0] = {}; // (object)
175 ...
176
177
178 Lambda Functions and Math
179 $ echo '{"t1":-30, "t2":-20, "t3":-10, "t4":0}' | jello '\
180 keys = _.keys()
181 vals = _.values()
182 cel = list(map(lambda x: (float(5)/9)*(x-32), vals))
183 dict(zip(keys, cel))'
184 {
185 "t1": -34.44444444444444,
186 "t2": -28.88888888888889,
187 "t3": -23.333333333333336,
188 "t4": -17.77777777777778
189 }
190
191
192
193 $ jc -a | jello 'len([entry for entry in _.parsers if "darwin" in entry.compatible])'
194 45
195
196
197 For Loops
198 Output as JSON array
199
200 $ jc -a | jello '\
201 result = []
202 for entry in _.parsers:
203 if "darwin" in entry.compatible:
204 result.append(entry.name)
205 result'
206 [
207 "airport",
208 "airport_s",
209 "arp",
210 "crontab",
211 "crontab_u",
212 ...
213 ]
214
215
216 Output as bash array
217
218 $ jc -a | jello -rl '\
219 result = []
220 for entry in _.parsers:
221 if "darwin" in entry.compatible:
222 result.append(entry.name)
223 result'
224 airport
225 airport_s
226 arp
227 crontab
228 crontab_u
229 ...
230
231
232 List and Dictionary Comprehension
233 Output as JSON array
234
235 $ jc -a | jello '[entry.name for entry in _.parsers if "darwin" in entry.compatible]'
236 [
237 "airport",
238 "airport_s",
239 "arp",
240 "crontab",
241 "crontab_u",
242 ...
243 ]
244
245
246 Output as bash array
247
248 $ jc -a | jello -rl '[entry.name for entry in _.parsers if "darwin" in entry.compatible]'
249 airport
250 airport_s
251 arp
252 crontab
253 crontab_u
254 ...
255
256
257 Environment Variables
258 $ echo '{"login_name": "joeuser"}' | jello '\
259 True if os.getenv("LOGNAME") == _.login_name else False'
260 true
261
262
263 Using 3rd Party Modules
264 You can import and use your favorite modules to manipulate the data.
265 For example, using glom:
266
267 $ jc -a | jello '\
268 from glom import *
269 glom(_, ("parsers", ["name"]))'
270 [
271 "airport",
272 "airport_s",
273 "arp",
274 "blkid",
275 "crontab",
276 "crontab_u",
277 "csv",
278 ...
279 ]
280
281
282
284 Custom Configuration File
285 You can use the -i option to initialize the jello environment with your
286 own configuration file. The configuration file accepts valid python
287 code where you can enable/disable jello options, customize your colors,
288 add import statements for your favorite modules, and define your own
289 functions.
290
291 The file must be named .jelloconf.py and must be located in the proper
292 directory based on the OS platform:
293
294 Linux, unix, macOS: ~/
295
296 Windows: %appdata%/
297
298 Setting Options
299 To set jello options in the .jelloconf.py file, import the
300 jello.lib.opts class, add any of the following and set to True or
301 False:
302
303 from jello.lib import opts
304 opts.mono = True # -m option
305 opts.compact = True # -c option
306 opts.lines = True # -l option
307 opts.raw = True # -r option
308 opts.force_color = True # -C option
309 opts.nulls = True # -n option
310 opts.schema = True # -s option
311 opts.types = True # -t option
312
313 Setting Colors
314 You can customize the colors by importing the jello.lib.opts class and
315 setting the following variables to one of the following string values:
316 black, red, green, yellow, blue, magenta, cyan, gray, brightblack,
317 brightred, brightgreen, brightyellow, brightblue, brightmagenta,
318 brightcyan, or white.
319
320 from jello.lib import opts
321 opts.keyname_color = 'blue' # Key names
322 opts.keyword_color = 'brightblack' # true, false, null
323 opts.number_color = 'magenta' # integers, floats
324 opts.string_color = 'green' # strings
325
326 Note: Any colors set via the JELLO_COLORS environment variable
327 will take precedence over any color values set in the .jel‐
328 loconf.py configuration file
329
330 Importing Modules
331 To import a module (e.g. glom) during initialization, just add the im‐
332 port statement to your .jelloconf.py file:
333
334 from glom import *
335
336 Then you can use glom in your jello filters without importing:
337
338 $ jc -a | jello -i 'glom(_, "parsers.25.name")'
339 "lsblk"
340
341 Adding Functions
342 You can also add functions to your initialization file. For example,
343 you could simplify glom use by adding the following function to .jel‐
344 loconf.py:
345
346 def g(q, data=_):
347 import glom
348 return glom.glom(data, q)
349
350 Then you can use the following syntax to filter the JSON data:
351
352 $ jc -a | jello -i 'g("parsers.6.compatible")'
353 [
354 "linux",
355 "darwin",
356 "cygwin",
357 "win32",
358 "aix",
359 "freebsd"
360 ]
361
362 Setting Custom Colors via Environment Variable
363 In addition to setting custom colors in the .jelloconf.py initializa‐
364 tion file, you can also set them via the JELLO_COLORS environment vari‐
365 able. Any colors set in the environment variable will take precedence
366 over any colors set in the initialization file.
367
368 The JELLO_COLORS environment variable takes four comma separated string
369 values in the following format:
370
371 JELLO_COLORS=<keyname_color>,<keyword_color>,<number_color>,<string_color>
372
373 Where colors are: black, red, green, yellow, blue, magenta, cyan, gray,
374 brightblack, brightred, brightgreen, brightyellow, brightblue, bright‐
375 magenta, brightcyan, white, or default
376
377 For example, to set to the default colors:
378
379 JELLO_COLORS=blue,brightblack,magenta,green
380
381 or
382
383 JELLO_COLORS=default,default,default,default
384
385
386 Disable Colors via Environment Variable
387 You can set the NO_COLOR environment variable to any value to disable
388 color output in jello. Note that using the -C option to force color
389 output will override both the NO_COLOR environment variable and the -m
390 option.
391
392
394 Kelly Brazil (kellyjonbrazil@gmail.com)
395
396 https://github.com/kellyjonbrazil/jello
397
398
400 Copyright (c) 2020-2021 Kelly Brazil
401
402 License: MIT License
403
404
405
4061.5.0 2021-12-08 jello(1)