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

如何修改Jolt转换Spec,实现单元素为对象、多元素为数组的输出?

Jolt转换问题:单元素属性转对象,多元素转数组

输入JSON

{
  "Notification": {
    "product": {
      "version": 1,
      "concepts": {
        "concept": [
          {
            "records": {
              "record": [
                {
                  "attributes": {
                    "attribute": [
                      {
                        "name": "IsListed",
                        "content": "N"
                      }
                    ]
                  }
                }
              ]
            },
            "cversion": 1,
            "cname": "ListingInfo"
          },
          {
            "records": {
              "record": [
                {
                  "attributes": {
                    "attribute": [
                      {
                        "name": "Code",
                        "content": "ABCD"
                      },
                      {
                        "name": "Value",
                        "content": "Vall"
                      }
                    ]
                  }
                }
              ]
            },
            "cversion": 1,
            "cname": "Reference"
          }
        ]
      }
    }
  }
}

期望输出

要求单个attribute时输出为对象,多个attribute时输出为数组:

{
  "version": 1,
  "ListingInfo": {
    "name": "IsListed",
    "content": "N"
  },
  "Reference": [
    {
      "name": "Code",
      "content": "ABCD"
    },
    {
      "name": "Value",
      "content": "Vall"
    }
  ]
}

当前使用的Spec

[
  {
    "operation": "shift",
    "spec": {
      "Notification": {
        "product": {
          "version": "version",
          "concepts": {
            "concept": {
              "*": {
                "records": {
                  "record": {
                    "*": {
                      "attributes": {
                        "attribute": {
                          "*": {
                            "name": "@(7,cname).[&1].&",
                            "content": "@(7,cname).[&1].&"
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
]

当前输出问题

所有属性都被转为数组,包括单个元素的情况:

{
  "version": 1,
  "ListingInfo": [
    {
      "name": "IsListed",
      "content": "N"
    }
  ],
  "Reference": [
    {
      "name": "Code",
      "content": "ABCD"
    },
    {
      "name": "Value",
      "content": "Vall"
    }
  ]
}

问题

需要修改Spec的哪些部分才能实现单个属性转对象、多个属性转数组的需求?


解决方案

在现有shift操作后添加cardinality操作,即可自动将单元素数组转为对象,同时保留多元素数组结构。

修改后的完整Spec如下:

[
  {
    "operation": "shift",
    "spec": {
      "Notification": {
        "product": {
          "version": "version",
          "concepts": {
            "concept": {
              "*": {
                "records": {
                  "record": {
                    "*": {
                      "attributes": {
                        "attribute": {
                          "*": {
                            "name": "@(7,cname).[&1].&",
                            "content": "@(7,cname).[&1].&"
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": "ONE_OR_MANY"
    }
  }
]

说明

  • cardinality操作的ONE_OR_MANY规则会自动处理:
    • 若字段值为单元素数组,转为单个对象;
    • 若为多元素数组,保持数组结构不变;
    • 若原本就是对象,无任何改动。
  • "*": "ONE_OR_MANY"表示对所有顶层字段应用该规则,刚好匹配ListingInfo和Reference的需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 16:55:10