File tree Expand file tree Collapse file tree 2 files changed +65
-0
lines changed Expand file tree Collapse file tree 2 files changed +65
-0
lines changed Original file line number Diff line number Diff line change @@ -260,8 +260,71 @@ Some examples:
260
260
]
261
261
` ` `
262
262
263
+ - Show all the keys and actions' length.
264
+
265
+ ```shell
266
+ $ jsonfmt -p ' {all_keys:keys(@), actions_len:length(actions)}' test/example.json
267
+ ```
268
+
269
+ *Output:*
270
+
271
+ ```json
272
+ {
273
+ "all_keys": [
274
+ "actions",
275
+ "age",
276
+ "gender",
277
+ "money",
278
+ "name"
279
+ ],
280
+ "actions_len": 2
281
+ }
282
+ ```
283
+
284
+ - Sort `actions` by `calorie` and redefine a dict.
285
+
286
+ ```shell
287
+ $ jsonfmt -p ' sort_by(actions, & calorie)[].{name: name, calorie:calorie}' test/example.json
288
+ ```
289
+
290
+ *Output:*
291
+
292
+ ```json
293
+ [
294
+ {
295
+ "name": "sport",
296
+ "calorie": -375
297
+ },
298
+ {
299
+ "name": "eat",
300
+ "calorie": 294.9
301
+ }
302
+ ]
303
+ ```
304
+
263
305
- [More examples](https://jmespath.org/examples.html).
264
306
307
+
308
+ **Amazingly**, you can do the same with YAML and TOML using JmesPath, and convert the result format arbitrarily.
309
+
310
+ ```shell
311
+ # read the data from toml file, and convert the result to yaml
312
+ $ jsonfmt -p ' {all_keys:keys(@), actions_len:length(actions)}' test/example.yaml -f toml
313
+ ```
314
+
315
+ *Output:*
316
+
317
+ ```yaml
318
+ all_keys:
319
+ - age
320
+ - gender
321
+ - money
322
+ - name
323
+ - actions
324
+ actions_len: 2
325
+ ```
326
+
327
+
265
328
### 5. Convert formats between JSON, TOML and YAML.
266
329
267
330
The *jsonfmt* can recognize any format of JSON, TOML and YAML from files or `stdin`. Either formats can be converted to the other by specifying the "-f" option.
Original file line number Diff line number Diff line change @@ -287,6 +287,8 @@ def main():
287
287
pops = pops )
288
288
except FormatError as err :
289
289
print_err (err )
290
+ except JMESPathError as err :
291
+ print_err (err )
290
292
except FileNotFoundError :
291
293
print_err (f'no such file: { file } ' )
292
294
except PermissionError :
You can’t perform that action at this time.
0 commit comments