#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)