Skip to content

Data Transform

CSV, JSON, XML, YAML parsing, generation, and pipeline transformations.

16 modules

ModuleDescription
讀取 CSV 檔案讀取並解析 CSV 檔案為物件陣列
寫入 CSV 檔案將物件陣列寫入 CSV 檔案
解析 JSON將 JSON 字串解析為物件
JSON 字串化將物件轉換為 JSON 字串
JSON 轉 CSV將 JSON 資料或檔案轉換為 CSV 格式
資料管道在單一步驟中串聯多個資料轉換
文字範本用變數填充文字範本
生成 XML從物件或陣列生成 XML 字串
解析 XML將 XML 字串解析為物件
生成 YAML從物件或陣列生成 YAML 字串
解析 YAML將 YAML 字串解析為物件
物件鍵取得物件的所有鍵
物件合併合併多個物件為一個
物件排除從物件中排除特定的鍵
物件選取從物件中選取特定的鍵
物件值取得物件的所有值

Modules

讀取 CSV 檔案

data.csv.read

讀取並解析 CSV 檔案為物件陣列

Parameters:

NameTypeRequiredDefaultDescription
pathstringYes-Path to the file
delimiterselect (,, ;, , `, `)No,
encodingselect (utf-8, ascii, latin-1, utf-16, gbk, big5)Noutf-8Character encoding for the file
skip_headerbooleanNoFalseSkip first row (header)

Output:

FieldTypeDescription
statusstring操作狀態
dataarray列物件陣列
rowsnumber列數
columnsarray欄位名稱列表

Example: Example

yaml
file_path: data/users.csv
delimiter: ,
encoding: utf-8

寫入 CSV 檔案

data.csv.write

將物件陣列寫入 CSV 檔案

Parameters:

NameTypeRequiredDefaultDescription
pathstringYes-Path to the file
dataarrayYes-Array of data items to process
delimiterselect (,, ;, , `, `)No,
encodingselect (utf-8, ascii, latin-1, utf-16, gbk, big5)Noutf-8Character encoding for the file

Output:

FieldTypeDescription
statusstring操作狀態
file_pathstring檔案路徑
rows_writtennumber寫入的列數

Example: Example

yaml
file_path: output/results.csv
data: [{"name": "John", "score": 95}, {"name": "Jane", "score": 87}]

解析 JSON

data.json.parse

將 JSON 字串解析為物件

Parameters:

NameTypeRequiredDefaultDescription
json_stringstringYes-JSON string to parse into an object or array

Output:

FieldTypeDescription
statusstring操作狀態
dataobject解析後的資料

Example: Example

yaml
json_string: {"name": "John", "age": 30}

JSON 字串化

data.json.stringify

將物件轉換為 JSON 字串

Parameters:

NameTypeRequiredDefaultDescription
dataobjectYes-Data object to process
prettybooleanNoFalseFormat with indentation
indentnumberNo2Indentation spaces (if pretty=true)

Output:

FieldTypeDescription
statusstring操作狀態
jsonstringJSON 字串

Example: Example

yaml
data: {"name": "John", "age": 30}
pretty: true

JSON 轉 CSV

data.json_to_csv

將 JSON 資料或檔案轉換為 CSV 格式

Parameters:

NameTypeRequiredDefaultDescription
input_dataanyYes-JSON data (array of objects) or path to JSON file
output_pathstringNo/tmp/output.csvPath where the output file will be saved
delimiterselect (,, ;, , `, `)No,
include_headerbooleanNoTrueInclude column headers in first row
flatten_nestedbooleanNoTrueFlatten nested objects using dot notation (e.g., address.city)
columnsarrayNo[]Specific columns to include (empty = all columns)

Output:

FieldTypeDescription
output_pathstring產生的 CSV 檔案路徑
row_countnumber列數
column_countnumber欄位數
columnsarray欄位名稱列表

Example: Convert JSON array to CSV

yaml
input_data: [{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]
output_path: /tmp/users.csv

Example: Convert JSON file

yaml
input_data: /path/to/data.json
output_path: /path/to/output.csv

資料管道

data.pipeline

在單一步驟中串聯多個資料轉換

Parameters:

NameTypeRequiredDefaultDescription
inputanyYes-要轉換的輸入資料(陣列或物件)
stepsarrayYes-要轉換的輸入資料(陣列或物件)

Output:

FieldTypeDescription
resultany依序應用的轉換步驟陣列
original_countinteger已轉換的資料
result_countinteger已轉換的資料
steps_appliedinteger轉換後的項目數量

Example: Example

yaml
input: ${input.users}
steps: [{"filter": {"field": "active", "condition": "eq", "value": true}}, {"sort": {"field": "name", "order": "asc"}}]

Example: Example

yaml
input: ${input.records}
steps: [{"map": {"extract": "id"}}, {"limit": 10}]

Example: Example

yaml
input: ${input.data}
steps: [{"filter": {"field": "status", "condition": "eq", "value": "completed"}}, {"pick": ["id", "name", "timestamp"]}, {"sort": {"field": "timestamp", "order": "desc"}}, {"skip": 5}, {"limit": 20}]

文字範本

data.text.template

用變數填充文字範本

Parameters:

NameTypeRequiredDefaultDescription
templatestringYes-Text template with {variable} placeholders
variablesobjectYes-Object with variable values

Output:

FieldTypeDescription
statusstring操作狀態
resultstring填充後的結果

Example: Example

yaml
template: Hello {name}, you scored {score} points!
variables: {"name": "Alice", "score": 95}

生成 XML

data.xml.generate

從物件或陣列生成 XML 字串

Parameters:

NameTypeRequiredDefaultDescription
dataobjectYes-要轉換為 XML 的資料
root_tagstringNoroot根元素標籤名稱
prettybooleanNoTrue美化列印 XML 輸出
encodingstringNoutf-8XML 輸出的字元編碼
declarationbooleanNoTrue包含 XML 宣告標頭

Output:

FieldTypeDescription
xmlstring生成的 XML 字串

Example: Example

yaml
data: {"user": {"@attributes": {"id": "1"}, "name": "John", "age": "30"}}
root_tag: users
pretty: true

解析 XML

data.xml.parse

將 XML 字串解析為物件

Parameters:

NameTypeRequiredDefaultDescription
contentstringNo-要解析的 XML 字串
file_pathstringNo-要解析的 XML 檔案路徑
preserve_attributesbooleanNoTrue在解析輸出中保留 XML 屬性

Output:

FieldTypeDescription
resultobject解析後的 XML 物件
root_tagstring根元素標籤名稱

Example: Example

yaml
content: <users><user id="1"><name>John</name></user></users>
preserve_attributes: true

生成 YAML

data.yaml.generate

從物件或陣列生成 YAML 字串

Parameters:

NameTypeRequiredDefaultDescription
dataanyYes-要轉換為 YAML 的資料
default_flow_stylebooleanNoFalse對巢狀結構使用流式風格
sort_keysbooleanNoFalse按字母順序排序鍵
indentnumberNo2縮排的空格數
allow_unicodebooleanNoTrue允許輸出中使用 Unicode 字元

Output:

FieldTypeDescription
yamlstring生成的 YAML 字串

Example: Example

yaml
data: {"name": "John", "age": 30, "cities": ["NYC", "LA"]}
sort_keys: false
indent: 2

解析 YAML

data.yaml.parse

將 YAML 字串解析為物件

Parameters:

NameTypeRequiredDefaultDescription
contentstringNo-要解析的 YAML 字串
file_pathstringNo-要解析的 YAML 檔案路徑
multi_documentbooleanNoFalse解析多文件 YAML(以 --- 分隔)

Output:

FieldTypeDescription
resultany解析後的 YAML 物件或陣列
typestring解析結果的類型

Example: Example

yaml
content: name: John
age: 30
cities:
  - NYC
  - LA
multi_document: false

Example: Example

yaml
content: ---
name: John
---
name: Jane
multi_document: true

物件鍵

object.keys

取得物件的所有鍵

Parameters:

NameTypeRequiredDefaultDescription
objectobjectYes-Input object/dictionary

Output:

FieldTypeDescription
keysarray物件鍵列表
countnumber鍵的數量

Example: Get object keys

yaml
object: {"name": "John", "age": 30, "city": "NYC"}

物件合併

object.merge

合併多個物件為一個

Parameters:

NameTypeRequiredDefaultDescription
objectsarrayYes-Array of objects to process

Output:

FieldTypeDescription
resultobject合併後的物件

Example: Merge user data

yaml
objects: [{"name": "John", "age": 30}, {"city": "NYC", "country": "USA"}, {"job": "Engineer"}]

物件排除

object.omit

從物件中排除特定的鍵

Parameters:

NameTypeRequiredDefaultDescription
objectobjectYes-Input object/dictionary
keysarrayYes-Keys to pick or omit

Output:

FieldTypeDescription
resultobject排除鍵後的物件

Example: Omit sensitive fields

yaml
object: {"name": "John", "age": 30, "password": "secret", "ssn": "123-45-6789"}
keys: ["password", "ssn"]

物件選取

object.pick

從物件中選取特定的鍵

Parameters:

NameTypeRequiredDefaultDescription
objectobjectYes-Input object/dictionary
keysarrayYes-Keys to pick or omit

Output:

FieldTypeDescription
resultobject只有選取鍵的物件

Example: Pick user fields

yaml
object: {"name": "John", "age": 30, "email": "john@example.com", "password": "secret"}
keys: ["name", "email"]

物件值

object.values

取得物件的所有值

Parameters:

NameTypeRequiredDefaultDescription
objectobjectYes-Input object/dictionary

Output:

FieldTypeDescription
valuesarray物件值列表
countnumber值的數量

Example: Get object values

yaml
object: {"name": "John", "age": 30, "city": "NYC"}

Released under the Apache 2.0 License.