#any->convert(from?: 'json'|yaml|'csv'|'markdown'|'latex', to?: 'json'|yaml|'csv'|html|'markdown'|'latex', header?: bool, as?: type, headers?: list, indented?: bool) → any
Changes the value's format. from: reads text written in a format (json, yaml, csv, markdown or latex), and to: writes the value in one (json, yaml, csv, or html, markdown and latex for documents); give both to turn one into the other. Reading csv:
not header when there is no header line. Reading json, yaml or csv: as: a type checks the data and turns it into records. Writing csv: headers: picks and orders the columns. Writing json: indented spreads it over lines. ->from(type:) and ->to(type:) are shortcuts for from: and to: on their own.| Parameter | Takes |
|---|---|
from optional | one of these words: 'json'|yaml|'csv'|'markdown'|'latex' |
to optional | one of these words: 'json'|yaml|'csv'|html|'markdown'|'latex' |
header optional | a flag: header or not header |
as optional | type |
headers optional | list |
indented optional | a flag: indented or not indented |
Arguments can be named: ->convert(…, to: …, header: …).
Example
<!-- 'apple': the json text is read into a list of maps -->
<echo>{ '[{"fruit": "apple"}]'->convert(from: json)->first().fruit }</echo>
<let name="prices" value="[{ fruit: 'apple', price: 3 }, { fruit: 'plum', price: 5 }]" />
<!-- fruit,price on the first line, then one line a row: the maps' keys name the columns -->
<echo>{ $prices->convert(to: csv) }</echo>
<!-- 'apple: 3': yaml read, then written as json -->
<echo>{ 'apple: 3'->convert(from: yaml, to: json) }</echo>// 'apple': the json text is read into a list of maps
echo '[{"fruit": "apple"}]'->convert(from: json)->first().fruit
$prices = [{ fruit: 'apple', price: 3 }, { fruit: 'plum', price: 5 }]
// fruit,price on the first line, then one line a row: the maps' keys name the columns
echo $prices->convert(to: csv)
// 'apple: 3': yaml read, then written as json
echo 'apple: 3'->convert(from: yaml, to: json)