Marcel now supports JSON input and output.
To convert a JSON document into a Python structure, use the `parse_json()` function.
A contrived example:
shell
bash ps | jc --ps | (p: json_parse(p)) | expand | (j: (j.pid, j.cmd))
* `bash ps | jc --ps` runs the bash `ps` command and then uses `jc --ps` to convert the output into a JSON document.
* `(p: json_parse(p))` converts the JSON into a Python structure.
* `expand` is a marcel operator that breaks a list into its elements. I.e., the input stream has one list with all the process info, and the output stream has a stream of structures each describing one process. Each structure has an object with fields describing process properties, e.g. `pid` and `cmd`.
* `(j: (j.pid, j.cmd))` extracts the `pid` and `cmd` of each process.
(This example is contrived because it can be done far more simply in marcel without JSON,
e.g. `ps | (p: (p.pid, p.command))`)
The mapping from JSON to Python is done by the Python `json` module. Consult documentation on
that module for details of the mapping.
Conversion of Python structures to JSON is done by the `json_format()` function. In this example,
maps of the form `{x: [x, x, x]}` are generated, for x = 0 ... 4. The structure is converted to a
JSON-formatted string by passing the stream, as a single list, to `json_format`. (By declaring the
parameter as `*m` instead of `m`, the stream of dicts is concatenated into a list of dicts.)
shell
({x: [x, x, x] for x in range(5)}) | (*m: json_format(m))