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

如何编写Jolt规格将嵌套对象转为数组并排除指定字段?

问题

如何编写Jolt规格,将给定的JSON输入转换为指定的期望输出?我不确定使用*操作符时,怎么实现嵌套对象转数组并排除"AllOf"字段。

输入JSON

{
  "data": {
    "properties": {
      "info": {
        "type": "object",
        "$comment": "Evaluation Information",
        "required": [
          "evalDate",
          "evalType",
          "evalWeather"
        ],
        "properties": {
          "evalDate": {
            "type": "string",
            "format": "date"
          },
          "evalType": {
            "type": "string",
            "enum": [
              "1st Party",
              "2nd Party",
              "3rd Party"
            ],
            "default": "2nd Party"
          },
          "evalWeather": {
            "type": "string"
          }
        }
      },
      "evaluator": {
        "type": "object",
        "required": [
          "id",
          "name"
        ],
        "properties": {
          "id": {
            "type": "string"
          },
          "name": {
            "type": "string"
          },
          "phone": {
            "type": [
              "string",
              "null"
            ]
          },
          "email": {
            "anyOf": [
              {
                "type": "string",
                "format": "email"
              },
              {
                "type": "null"
              }
            ]
          }
        }
      },
      "AllOf": []
    }
  }
}

期望输出JSON

{
  "FormModel": {
    "Categories": [
      {
        "Title": "info",
        "fields": [
          {
            "Title": "evalDate",
            "DataType": "string"
          },
          {
            "Title": "evalType",
            "DataType": "string"
          },
          {
            "Title": "evalWeather",
            "DataType": "string"
          }
        ]
      },
      {
        "Title": "evaluator",
        "fields": [
          {
            "Title": "id",
            "DataType": "string"
          },
          {
            "Title": "name",
            "DataType": "string"
          },
          {
            "Title": "phone",
            "DataType": [
              "string"
            ]
          },
          {
            "Title": "email",
            "DataType": [
              "string",
              "null"
            ]
          }
        ]
      }
    ]
  }
}
解决方案

可以通过以下Jolt规格实现转换,核心逻辑是排除指定字段、将嵌套对象转为数组、处理特殊类型的字段提取:

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "properties": {
          // 直接排除AllOf字段,不参与后续映射
          "AllOf": null,
          // 遍历所有非AllOf的分类节点
          "*": {
            "@(0,key)": "FormModel.Categories[&1].Title",
            "properties": {
              // 遍历分类下的每个字段
              "*": {
                "@(0,key)": "FormModel.Categories[&3].fields[&1].Title",
                // 处理anyOf类型,提取所有子项的type值到数组
                "anyOf": {
                  "*": {
                    "type": "FormModel.Categories[&5].fields[&3].DataType[]"
                  }
                },
                // 处理普通type字段,过滤null值(对应phone字段需求)
                "type": {
                  "null": null,
                  "*": "FormModel.Categories[&4].fields[&2].DataType"
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "FormModel": {
        "Categories": {
          "*": {
            "fields": {
              "*": {
                // 把单个字符串类型的DataType转为数组,统一格式
                "DataType": ["=split('',@(1,DataType))", "=join('',@(1,DataType))"]
              }
            }
          }
        }
      }
    }
  }
]

规格细节说明

  1. Shift操作:

    • 用"AllOf": null直接跳过该字段的映射
    • 通过*通配符遍历所有分类节点,将节点名赋值给Title
    • 遍历分类下的字段,将字段名赋值给fields的Title
    • 针对anyOf类型,遍历子项收集所有type值形成数组
    • 针对普通type字段,过滤掉null值,只保留有效类型
  2. Modify操作:

    • 统一DataType的格式,将单个字符串转为数组,确保输出格式一致(如果不需要统一格式可省略此步骤)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 21:50:53