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