如何编写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 }
脚本逻辑说明
- 整体结构保留:保留原始JSON的
valuesToUpload和reload顶级字段 - 分组处理:遍历每个
valuesToUpload中的分组:- banner组处理:
- 提取分组的
groupId和groupTitle - 遍历分组内的每个field,将公共的
fieldType存储为变量 - 展开
imageValue数组,每个数组项生成一个独立的field对象:- 带上公共的
fieldType - 提取数组项的
valueId和toUpload - 将数组项的
imageId、imageSchema、url包装为imageValue对象
- 带上公共的
- 提取分组的
- 非banner组处理:按原有逻辑过滤掉
valueId为null的字段,保留原始结构
- banner组处理:
转换后完整结果
{ "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
相关产品推荐
相关产品推荐

