Skip to content

Commit

Permalink
[doc] Add example expression to JSON doc
Browse files Browse the repository at this point in the history
Addresses issue #1129.
  • Loading branch information
Andy C committed May 19, 2022
1 parent fd6ebaf commit bfd9c6f
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion doc/json.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Examples:

# 'json read' is valid at the end of a pipeline (because Oil implements
# shopt -s lastpipe)
echo '{"count": 42}' | json read :myvar
$ echo '{"count": 42}' | json read :myvar

# Failure with invalid input data
$ echo '[ "incomplete"' | json read :myvar < invalid.json
Expand Down Expand Up @@ -95,6 +95,28 @@ Notes:

- `--indent` is ignored if `--pretty` is false.

## Filter Data Structures with Oil Expressions

Once your data is deserialized, you can use Oil expression to operate on it.

$ echo '{"counts": [42, 99]}' | json read :d

$ = d['counts']
(List) [42, 99]

$ = d['counts'][1]
(Int) 99

# d->counts is a synonym for d["counts"]
$ json write (d->counts)
[
42,
99
]

Note: It may more efficient to filter large data structures with tools like
`jq` first.

## Other Data Structures Can Be Printed as JSON

Oil arrays and shell arrays both serialize to a list of strings:
Expand Down

0 comments on commit bfd9c6f

Please sign in to comment.