← 返回主平台

概述

转换规则用于改变数据的格式和类型,支持:

  • 日期格式转换
  • 数值和字符串转换
  • 布尔映射
  • 字段拆分和合并
  • 编码转换(URL、Base64 等)

基本结构

规则配置

{
  "type": "convert",
  "field": "字段名",
  "subType": "转换类型"  // str_to_num, num_to_str, bool_to_num, date_format, unit_conversion
}

字符串与数字转换

字符串转数字

{
  "type": "convert",
  "field": "price",
  "subType": "str_to_num"
}

// "99.99" → 99.99
// "100" → 100

数字转字符串

{
  "type": "convert",
  "field": "zip_code",
  "subType": "num_to_str"
}

// 10001 → "10001"
// 5 → "5"

布尔与数字转换

布尔转数字

{
  "type": "convert",
  "field": "subscribed",
  "subType": "bool_to_num"
}

// true → 1
// false → 0

数字转布尔

{
  "type": "convert",
  "field": "status",
  "subType": "num_to_bool"
}

// 1 → true
// 0 → false

日期格式转换

自动识别并标准化日期

{
  "type": "convert",
  "field": "date",
  "subType": "date_format"
}

// "2024/03/23" → "2024-03-23"
// "23-Mar-2024" → "2024-03-23"
// "2024年3月23日" → "2024-03-23"
// 会自动转换为标准 YYYY-MM-DD 格式

单位转换

长度单位转换

{
  "type": "convert",
  "field": "height",
  "subType": "unit_conversion",
  "fromUnit": "cm",
  "toUnit": "m"
}

// 175 → 1.75

温度单位转换

{
  "type": "convert",
  "field": "temperature",
  "subType": "unit_conversion",
  "fromUnit": "celsius",
  "toUnit": "fahrenheit"
}

// 0 → 32
// 100 → 212

支持的单位对

// 长度:m, cm, mm, km, inch, foot, yard, mile
// 温度:celsius, fahrenheit, kelvin
// 重量:kg, g, mg, ton, lb, oz
// 速度:m/s, km/h, mph
// 体积:liter, ml, gallon, pint

实战示例

例 1:标准化用户数据

[
  { "type": "convert", "field": "age", "subType": "str_to_num" },
  { "type": "convert", "field": "signup_date", "subType": "date_format" },
  { "type": "convert", "field": "is_active", "subType": "bool_to_num" }
]

// age: "25" → 25
// signup_date: "2024/01/15" → "2024-01-15"
// is_active: true → 1

例 2:健康数据转换

[
  { "type": "convert", "field": "height_cm", "subType": "unit_conversion", "fromUnit": "cm", "toUnit": "m" },
  { "type": "convert", "field": "weight_lb", "subType": "unit_conversion", "fromUnit": "lb", "toUnit": "kg" }
]

// height_cm: 175 → 1.75
// weight_lb: 154 → 70

例 3:销售数据清理

[
  { "type": "convert", "field": "amount", "subType": "str_to_num" },
  { "type": "convert", "field": "quantity", "subType": "str_to_num" },
  { "type": "convert", "field": "order_date", "subType": "date_format" }
]
💡 下一步:学习计算规则生成衍生字段和指标。