Skip to content

Commit 3453764

Browse files
committed
update readme and fix bugs
1 parent 14a2046 commit 3453764

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,71 @@ Some examples:
260260
]
261261
```
262262

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+
263305
- [More examples](https://jmespath.org/examples.html).
264306
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+
265328
### 5. Convert formats between JSON, TOML and YAML.
266329
267330
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.

jsonfmt.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,8 @@ def main():
287287
pops=pops)
288288
except FormatError as err:
289289
print_err(err)
290+
except JMESPathError as err:
291+
print_err(err)
290292
except FileNotFoundError:
291293
print_err(f'no such file: {file}')
292294
except PermissionError:

0 commit comments

Comments
 (0)