#any->convert(from?: 'json'|yaml|'csv'|'markdown'|'latex', to?: 'json'|yaml|'csv'|html|'markdown'|'latex', header?: bool, as?: type, headers?: list, indented?: bool) → any
Cambia il formato del valore. from: legge un testo scritto in un formato (json, yaml, csv, markdown o latex), e to: scrive il valore in un formato (json, yaml, csv, oppure html, markdown e latex per i documenti); indicali entrambi per convertire un formato in un altro. Lettura di csv:
not header quando non c'è una riga di intestazione. Lettura di json, yaml o csv: as: un tipo controlla i dati e li trasforma in record. Scrittura di csv: headers: sceglie e ordina le colonne. Scrittura di json: indented li distribuisce su più righe. ->from(type:) e ->to(type:) sono scorciatoie per from: e to: da soli.| Parametro | Accetta |
|---|---|
from facoltativo | una di queste parole: 'json'|yaml|'csv'|'markdown'|'latex' |
to facoltativo | una di queste parole: 'json'|yaml|'csv'|html|'markdown'|'latex' |
header facoltativo | un flag: header oppure not header |
as facoltativo | type |
headers facoltativo | list |
indented facoltativo | un flag: indented o not indented |
Gli argomenti possono essere con nome: ->convert(…, to: …, header: …).
Esempio
<!-- '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)