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

使用Jolt转换JSON:将字符串列表拆分转为指定键值对

使用Jolt转换JSON数据:字符串列表转键值对

修正后的输入JSON(原输入存在重复键问题,已合并为有效数组)

{
  "id": "abc",
  "type": "car",
  "size": "small",
  "carInformation": [
    {
      "Licence": {
        "code": "AP123"
      },
      "additionalInformation": [
        "Brought successfully",
        "/identification/",
        "/Rate/30000",
        "/Date/2023-01-11"
      ]
    },
    {
      "Licence": {
        "code": "AP124"
      },
      "additionalInformation": [
        "Brought unsuccessfully",
        "/identification/98981",
        "/Rate/30030",
        "/Date/2023-01-12"
      ]
    }
  ]
}

需求说明

  • 遍历carInformation数组及其中的additionalInformation数组
  • 按/拆分字符串:
    • 拆分后提取键值(如/Rate/30000 → 键Rate,值30000),同键值收集为数组
    • 不含/的字符串,按出现顺序以DATA{序号}为键存储内容

Jolt转换规范

[
  {
    "operation": "shift",
    "spec": {
      "carInformation": {
        "*": {
          "additionalInformation": {
            "*": {
              "/": {
                "@1": {
                  "/.*?/.*": {
                    "$(0,1)": "[&3]",
                    "$(0,2)": "@"
                  },
                  "/.*?/": {
                    "$(0,1)": "[&3]",
                    "#null": "@"
                  }
                }
              },
              "!/": {
                "$": "DATA[]"
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "&1.&",
        "DATA": {
          "*": "&2.DATA$(1,1)"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "cars.carInformation.&"
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "cars": {
        "carInformation": {
          "identification": {
            "*": ["=notNull", null]
          }
        }
      }
    }
  }
]

转换说明

  1. 第一步Shift:展开数组,拆分含/的字符串提取键值,将无/的字符串收集到DATA数组
  2. 第二步Shift:将DATA数组转为DATA1、DATA2格式的键值对
  3. 第三步Shift:把所有结果包装到cars.carInformation结构中
  4. 第四步Modify:处理空值(如第一个identification的空内容,设置为null;若不需要可删除此步骤)

输出结果

{
  "cars": {
    "carInformation": {
      "identification": [null, "98981"],
      "Rate": ["30000", "30030"],
      "Date": ["2023-01-11", "2023-01-12"],
      "DATA1": "Brought successfully",
      "DATA2": "Brought unsuccessfully"
    }
  }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 02:37:47