You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Jolt中批量转换任意层级的字符串布尔值为布尔类型?

批量转换JSON中所有字符串布尔值为原生布尔值(Jolt实现)

无需显式指定属性名,通过Jolt的递归遍历+类型转换规则,自动处理所有层级的字符串类型布尔值,同时完整保留原有数据结构和其他属性值。

Jolt转换Spec

[
  {
    "operation": "shift",
    "spec": {
      "**": {
        "@": {
          "true": { "$": "&2" },
          "false": { "$": "&2" },
          "*": { "$": "&2" }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "**": {
        "true": "=toBoolean",
        "false": "=toBoolean"
      }
    }
  }
]

输入示例

{
  "name": "Fred",
  "age": 45,
  "opentowork" : "true",
  "friends" : [
    {
      "name": "penny",
      "closefriend": "false"
    },
    {
      "name": "roger",
      "farfriend": "true"
    }
  ]
}

输出示例

{
  "name": "Fred",
  "age": 45,
  "opentowork" : true,
  "friends" : [
    {
      "name": "penny",
      "closefriend": false
    },
    {
      "name": "roger",
      "farfriend": true
    }
  ]
}

规则说明

  1. Shift操作(递归保留结构):

    • "**" 通配符递归匹配JSON中所有层级的属性节点
    • 对每个属性的值进行判断:若为"true"/"false"则保留原键值对,其他值也原样保留,确保完整复制原有数据结构
  2. Modify操作(类型转换):

    • 再次通过"**"递归遍历所有节点
    • 调用=toBoolean函数,将字符串"true"转为原生布尔值true,字符串"false"转为原生布尔值false,其他非目标值不受影响

内容的提问来源于stack exchange,提问作者ArthwitRail

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 22:15:49