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

如何使用Jolt将JSON数组转换为独立的JSON对象数组?

Jolt转换问题:将数组对象映射为独立Person对象

我需要把一个JSON对象数组转换成符合指定Schema的新JSON对象数组,作为Jolt新手,当前输出不符合预期,试过[&1]和@运算符仍未得到目标结果。

输入JSON

{
  "totalCount": 2,
  "entities": {
    "people": [
      {
        "uniqueIdentifier": "0105",
        "common": {
          "firstName": "Bob",
          "lastName": "Smith",
          "geoLocation": {
            "geometry": {
              "point": {
                "coordinates": [
                  39.93479016,
                  47.21850072
                ]
              }
            }
          },
          "timeOfReporting": "2019-11-18T18:36:10Z"
        },
        "type": "PERSON",
        "parent": {
          "firstName": "Jane",
          "lastName": "Smith"
        }
      },
      {
        "uniqueIdentifier": "0106",
        "common": {
          "firstName": "Joe",
          "lastName": "Green",
          "geoLocation": {
            "geometry": {
              "point": {
                "coordinates": [
                  39.93479016,
                  47.21850072
                ]
              }
            }
          },
          "timeOfReporting": "2019-11-18T18:36:10Z"
        },
        "Type": "PERSON",
        "parent": {
          "firstName": "John",
          "lastName": "Green"
        }
      }
    ]
  }
}

当前使用的Jolt Spec

[
  {
    "operation": "shift",
    "spec": {
      "entities": {
        "people": {
          "*": {
            "uniqueIdentifier": "Person.Id",
            "type": "Person.Type",
            "parent": {
              "firstName": "Person.Parent.FirstName",
              "lastName": "Person.Parent.LastName"
            },
            "common": {
              "firstName": "Person.FirstName",
              "lastName": "Person.LastName",
              "timeOfReporting": "Person.DateCreated",
              "geoLocation": {
                "geometry": {
                  "point": {
                    "coordinates": {
                      "0": "Person.Locations.Location.Longitude",
                      "1": "Person.Locations.Location.Latitude"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "default",
    "spec": {
      "Person": {
        "Processed": "false"
      }
    }
  }
]

当前输出结果

{
  "Person" : {
    "DateCreated" : [ "2019-11-18T18:36:10Z", "2019-11-18T18:36:10Z" ],
    "FirstName" : [ "Bob", "Joe" ],
    "Id" : [ "0105", "0106" ],
    "LastName" : [ "Smith", "Green" ],
    "Locations" : {
      "Location" : {
        "Latitude" : [ 47.21850072, 47.21850072 ],
        "Longitude" : [ 39.93479016, 39.93479016 ]
      }
    },
    "Parent" : {
      "FirstName" : [ "Jane", "John" ],
      "LastName" : [ "Smith", "Green" ]
    },
    "Processed" : "false",
    "Type" : "PERSON"
  }
}

期望输出结果

{
    "Person":{
        "DateCreated": "2019-11-18T18:36:10Z",
        "FirstName": "Bob",
        "LastName" : "Smith",
        "Id": "0105",
        "Locations": {
            "Location": {
                "Latitude": 47.2184,
                "Longitude": 39.7854
            }
        },
        "Parent": {
            "FirstName": "Jane",
            "LastName": "Smith"
        },
        "Processed": "false",
        "Type": "PERSON"
    },
    "Person": {
        "DateCreated": "2019-11-18T18:36:10Z",
        "FirstName": "Joe",
        "LastName": "Green",
        "Id": "0106",
        "Locations": {
            "Location": {
                "Latitude": 42.2184,
                "Longitude": 31.7854
            }
        },
        "Parent": {
            "FirstName": "John",
            "LastName": "Green"
        },
        "Processed": "false",
        "Type": "PERSON"
    }
}

修正方案及说明

重要前提

JSON规范不允许同一对象内存在重复键(比如两个"Person"),实际解析时第二个Person会覆盖第一个,因此更合理的输出是将多个Person对象放入数组中。以下给出符合规范的修正方案:

修正后的Jolt Spec

[
  {
    "operation": "shift",
    "spec": {
      "entities": {
        "people": {
          "*": {
            // 用[&1]保留数组索引,将每个people元素映射为独立对象
            "uniqueIdentifier": "Persons[&1].Id",
            "type|Type": "Persons[&1].Type", // 兼容大小写不同的type字段
            "parent": {
              "firstName": "Persons[&2].Parent.FirstName",
              "lastName": "Persons[&2].Parent.LastName"
            },
            "common": {
              "firstName": "Persons[&2].FirstName",
              "lastName": "Persons[&2].LastName",
              "timeOfReporting": "Persons[&2].DateCreated",
              "geoLocation": {
                "geometry": {
                  "point": {
                    "coordinates": {
                      "0": "Persons[&4].Locations.Location.Longitude",
                      "1": "Persons[&4].Locations.Location.Latitude"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "default",
    "spec": {
      "Persons": {
        "*": {
          "Processed": "false"
        }
      }
    }
  }
]

关键修正点

  1. 保留独立对象:通过Persons[&1](&1代表当前数组层级的索引),将每个people元素映射为Persons数组中的独立对象,避免字段被合并成数组。
  2. 层级匹配:嵌套字段中使用&2、&4等索引,对应父级的数组层级,确保每个字段正确关联到对应的Person对象。
  3. 兼容大小写字段:用type|Type匹配输入中大小写不一致的类型字段,避免第二个对象的类型信息丢失。
  4. Default操作调整:针对Persons数组中的每个对象添加Processed: false,而非给单个Person对象添加。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 02:55:17