#any->convert(from?: 'json'|yaml|'csv'|'markdown'|'latex', to?: 'json'|yaml|'csv'|html|'markdown'|'latex', header?: bool, as?: type, headers?: list, indented?: bool) → any
Ändert das Format des Wertes. from: liest Text in einem Format (json, yaml, csv, markdown oder latex), und to: schreibt den Wert in einem Format (json, yaml, csv oder html, markdown und latex für Dokumente); gib beides an, um das eine in das andere umzuwandeln. Beim Lesen von csv:
not header, wenn keine Kopfzeile vorhanden ist. Beim Lesen von json, yaml oder csv: as: ein Typ prüft die Daten und wandelt sie in Datensätze um. Beim Schreiben von csv: headers: wählt die Spalten aus und ordnet sie. Beim Schreiben von json: indented verteilt die Ausgabe auf mehrere Zeilen. ->from(type:) und ->to(type:) sind Kurzschreibweisen für from: und to: allein.| Parameter | Nimmt |
|---|---|
from optional | eines dieser Wörter: 'json'|yaml|'csv'|'markdown'|'latex' |
to optional | eines dieser Wörter: 'json'|yaml|'csv'|html|'markdown'|'latex' |
header optional | ein Flag: header oder not header |
as optional | type |
headers optional | list |
indented optional | ein Schalter: indented oder not indented |
Argumente können benannt werden: ->convert(…, to: …, header: …).
Beispiel
<!-- '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)