使用 Jolt 变换时进行反击

Counter while transforming with Jolt

我正在将两个对象数组转换为一个对象数组。这样做时,根据 item 的类型,我需要为每个项目分配一个 id。并且,将项目放入单个数组后,id 应该是连续的,按照它们最初出现在输入 json 中的顺序。它不是所有项目的单个 itemId。它基于它自己的类型。 PFB 输入和输出 JSON 个样本。使用 jolt-core [0.1.0] 。请帮忙

输入Json:

{
    "Beverages" : {
        "Items" : [ {
            "name" : "cofee",
            "type" : "hot"
        },{
            "name" : "wine",
            "type" : "cold"
        },{
            "name" : "tea",
            "type" : "hot"
        },{
            "name" : "beer",
            "type" : "cold"
        }
        ]   
    },
    "Snacks" : {
        "Items" : [ {
            "name" : "crackers",
            "type" : "hot"
        },{
            "name" : "salad",
            "type" : "cold"
        },{
            "name" : "muffin",
            "type" : "cold"
        },{
            "name" : "toast",
            "type" : "hot"
        }
        ]
    }
}

而且,预期的输出是:

{
    "items" : [{
            "name" : "cofee",
            "type" : "hot",
            "hotId" : 1
        },{
            "name" : "wine",
            "type" : "cold",
            "coldId" : 1
        },{
            "name" : "tea",
            "type" : "hot",
            "hotId" : 2
        },{
            "name" : "beer",
            "type" : "cold",
            "coldId" : 2
        },{
            "name" : "crackers",
            "type" : "hot",
            "hotId" : 3
        },{
            "name" : "salad",
            "type" : "cold",
            "coldId" : 3
        },{
            "name" : "muffin",
            "type" : "cold",
            "coldId" : 4
        },{
            "name" : "toast",
            "type" : "hot",
            "hotId" : 4
        }
    
    ]
}

如上,hotId和coldId是输出中的新字段。但是,它会增加。

这个(冗长的)规范应该产生输出

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "Items": {
          "*": {
            "name": "@(1,type)"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "&1[&0].name"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": "&2[&1].&",
          "": "&2[&1].type"
        }
      }
    }
  },
  {
    "operation": "modify-default-beta",
    "spec": {
      "*": {
        "*": {
          "idName": "=concat(@(1,type), 'Id')"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "@": "&",
        "*": {
          "idName": {
            "": "&3[&2].idValue"
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": {
          "idValue": "=intSum(@(1,idValue), 1)"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "idValue": "&2[&1].@(1,idName)"
        },
        "@": "&"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "name|type|coldId|hotId": "&2[&1].&"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "items[]"
      }
    }
  }
]

尝试一个接一个地检查转换的输出。 注意:元素的顺序与所需输出中的顺序不同。如果我找到更多时间,我会尝试整理它并(可能)使其更简洁。