#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 el formato del valor. from: lee texto escrito en un formato (json, yaml, csv, markdown o latex), y to: escribe el valor en uno (json, yaml, csv, o html, markdown y latex para documentos); indica ambos para convertir uno en otro. Lectura de csv:
not header cuando no hay línea de encabezado. Lectura de json, yaml o csv: as: un tipo valida los datos y los convierte en registros. Escritura de csv: headers: elige y ordena las columnas. Escritura de json: indented los distribuye en líneas. ->from(type:) y ->to(type:) son atajos de from: y to: por separado.| Parámetro | Recibe |
|---|---|
from opcional | una de estas palabras: 'json'|yaml|'csv'|'markdown'|'latex' |
to opcional | una de estas palabras: 'json'|yaml|'csv'|html|'markdown'|'latex' |
header opcional | una marca: header o not header |
as opcional | type |
headers opcional | list |
indented opcional | un indicador: indented o not indented |
Los argumentos pueden tener nombre: ->convert(…, to: …, header: …).
Ejemplo
<!-- '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)