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

如何编写JQ Filter统一about与banner组的image字段格式?

统一图片字段结构的JQ解决方案

问题背景

数据提供商返回的JSON中,valuesToUpload包含两组结构不同的数据,核心差异在imageValue字段:

  • about组(如about_2):imageValue为单个对象
  • banner组:imageValue为对象数组

需要将banner组的结构重构为与about组一致,同时保留about组的有效数据(过滤掉valueId为null的字段)。

原始数据

{
  "valuesToUpload": [
    {
      "groupId": "about_2",
      "groupTitle": "",
      "fields": [
        {
          "fieldType": "image",
          "valueId": "cd262467-e683-4b1a-8fff-b22c42722f2c",
          "toUpload": true,
          "imageValue": {
            "imageId": 27606887612,
            "imageSchema": "profile_composition",
            "url": "http://img10/image/1/1.YaE0cFYle0oRLlTPlS7HAwtEINoMkYFRuB_k"
          }
        },
        {
          "fieldType": "image",
          "fieldName": "example_images",
          "valueId": "5b394732-28c1-424c-a5c8-5edd553a3711",
          "toUpload": true,
          "imageValue": {
            "imageId": 27606306642,
            "imageSchema": "profile_composition",
            "url": "http://img40/image/1/1.j_RTsLassXjnkzZKMn-0czdeML_SxCdo-c"
          }
        },
        {
          "fieldType": "image",
          "fieldName": "example_images",
          "valueId": "efaeca0d-9dbe-4b1c-93a1-51cb54b9ec1b",
          "toUpload": true,
          "imageValue": {
            "imageId": 27606343705,
            "imageSchema": "profile_composition",
            "url": "http://img00/image/1/1.xNLAhbas-l50pn65eZTqgqHrazl2.r39lMZMqeTovl4Cj2EY"
          }
        }
      ]
    },
    {
      "groupId": "banner",
      "groupTitle": "banner",
      "fields": [
        {
          "fieldType": "image",
          "imageValue": [
            {
              "imageId": 1,
              "toUpload": true,
              "valueId": "efaeca0d-9dbe-4b1c-93a1-51cb54b9ec00",
              "imageSchema": "profile_composition",
              "url": "https://20.img.st/image/1/link1"
            },
            {
              "imageId": 2,
              "toUpload": false,
              "valueId": "efaeca0d-9dbe-4b1c-93a1-51cb54b9ec01",
              "imageSchema": "profile_composition",
              "url": "https://20.img.st/image/1/link2"
            }
          ],
          "fieldName": "banner",
          "fieldTitle": "banner"
        }
      ]
    }
  ],
  "reload": {
    "on": false
  }
}

目标格式

banner组需转换为与about组一致的结构,每个imageValue数组项对应一个独立的field对象:

{
  "groupId": "banner",
  "groupTitle": "banner",
  "fields": [
    {
      "fieldType": "image",
      "valueId": "efaeca0d-9dbe-4b1c-93a1-51cb54b9ec00",
      "toUpload": true,
      "imageValue": {
        "imageId": 1,
        "imageSchema": "profile_composition",
        "url": "https://20.img.st/image/1/link1"
      }
    },
    {
      "fieldType": "image",
      "valueId": "efaeca0d-9dbe-4b1c-93a1-51cb54b9ec01",
      "toUpload": false,
      "imageValue": {
        "imageId": 2,
        "imageSchema": "profile_composition",
        "url": "https://20.img.st/image/1/link2"
      }
    }
  ]
}

JQ过滤脚本

{
  valuesToUpload: [
    .valuesToUpload[] |
    if .groupId == "banner" then
      {
        groupId,
        groupTitle,
        fields: [
          .fields[] |
          .fieldType as $type |
          .imageValue[] |
          {
            fieldType: $type,
            valueId,
            toUpload,
            imageValue: {
              imageId,
              imageSchema,
              url
            }
          }
        ]
      }
    else
      {groupId, groupTitle, fields: [.fields[] | select(.valueId != null)]}
    end
  ],
  reload: .reload
}

脚本逻辑说明

  1. 整体结构保留:保留原始JSON的valuesToUpload和reload顶级字段
  2. 分组处理:遍历每个valuesToUpload中的分组:
    • banner组处理:
      • 提取分组的groupId和groupTitle
      • 遍历分组内的每个field,将公共的fieldType存储为变量
      • 展开imageValue数组,每个数组项生成一个独立的field对象:
        • 带上公共的fieldType
        • 提取数组项的valueId和toUpload
        • 将数组项的imageId、imageSchema、url包装为imageValue对象
    • 非banner组处理:按原有逻辑过滤掉valueId为null的字段,保留原始结构

转换后完整结果

{
  "valuesToUpload": [
    {
      "groupId": "about_2",
      "groupTitle": "",
      "fields": [
        {
          "fieldType": "image",
          "valueId": "cd262467-e683-4b1a-8fff-b22c42722f2c",
          "toUpload": true,
          "imageValue": {
            "imageId": 27606887612,
            "imageSchema": "profile_composition",
            "url": "http://img10/image/1/1.YaE0cFYle0oRLlTPlS7HAwtEINoMkYFRuB_k"
          }
        },
        {
          "fieldType": "image",
          "fieldName": "example_images",
          "valueId": "5b394732-28c1-424c-a5c8-5edd553a3711",
          "toUpload": true,
          "imageValue": {
            "imageId": 27606306642,
            "imageSchema": "profile_composition",
            "url": "http://img40/image/1/1.j_RTsLassXjnkzZKMn-0czdeML_SxCdo-c"
          }
        },
        {
          "fieldType": "image",
          "fieldName": "example_images",
          "valueId": "efaeca0d-9dbe-4b1c-93a1-51cb54b9ec1b",
          "toUpload": true,
          "imageValue": {
            "imageId": 27606343705,
            "imageSchema": "profile_composition",
            "url": "http://img00/image/1/1.xNLAhbas-l50pn65eZTqgqHrazl2.r39lMZMqeTovl4Cj2EY"
          }
        }
      ]
    },
    {
      "groupId": "banner",
      "groupTitle": "banner",
      "fields": [
        {
          "fieldType": "image",
          "valueId": "efaeca0d-9dbe-4b1c-93a1-51cb54b9ec00",
          "toUpload": true,
          "imageValue": {
            "imageId": 1,
            "imageSchema": "profile_composition",
            "url": "https://20.img.st/image/1/link1"
          }
        },
        {
          "fieldType": "image",
          "valueId": "efaeca0d-9dbe-4b1c-93a1-51cb54b9ec01",
          "toUpload": false,
          "imageValue": {
            "imageId": 2,
            "imageSchema": "profile_composition",
            "url": "https://20.img.st/image/1/link2"
          }
        }
      ]
    }
  ],
  "reload": {
    "on": false
  }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 21:40:55