#any->convert(from?: 'json'|yaml|'csv'|'markdown'|'latex', to?: 'json'|yaml|'csv'|html|'markdown'|'latex', header?: bool, as?: type, headers?: list, indented?: bool) → any
Меняет формат значения. from: читает текст, записанный в одном из форматов (json, yaml, csv, markdown или latex), а to: записывает значение в один из них (json, yaml, csv, либо html, markdown и latex для документов); задайте оба, чтобы преобразовать одно в другое. Чтение csv:
not header, если в данных нет строки заголовков. Чтение json, yaml или csv: as: задаёт тип, проверяет данные и превращает их в записи. Запись csv: headers: выбирает и упорядочивает столбцы. Запись json: indented разбивает вывод на строки с отступами. ->from(type:) и ->to(type:) — сокращения для from: и to: по отдельности.| Параметр | Принимает |
|---|---|
from необязательный | одно из слов: 'json'|yaml|'csv'|'markdown'|'latex' |
to необязательно | одно из слов: 'json'|yaml|'csv'|html|'markdown'|'latex' |
header необязательный | флаг: header или not header |
as необязательно | type |
headers необязательно | list |
indented необязательно | флаг: indented или not indented |
Аргументы можно передавать по имени: ->convert(…, to: …, header: …).
Пример
<!-- '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)