Jolt Spec - 展平嵌套数组并复制根值

Jolt Spec - Flatten out Nested Array and copy root values

我需要展平嵌套数组并将值从外部节点复制到展平数组中的每个元素。

需要将 productSizes 嵌套数组展平为数据数组,然后需要将产品节点中的字段添加到数据数组,同时保持关系。

输入JSON:

[
  {
    "product": {
      "productSizes": [
        {
          "productCode": "AA0714-001",
          "masterSizeCode": "1.5Y"
        },
        {
          "productCode": "AA0714-001",
          "masterSizeCode": "11C"
        }
      ],
      "genderAgeCode": "20",
      "divisionCode": "20"
    }
  },
  {
    "product": {
      "productSizes": [
        {
          "productCode": "BB0204-100",
          "masterSizeCode": "XL"
        },
        {
          "productCode": "BB0204-100",
          "masterSizeCode": "S"
        }
      ],
      "genderAgeCode": "01",
      "divisionCode": "30"
    }
  }
]

预期输出JSON:

{
  "Data" : [ {
    "productCode" : "AA0714-001",
    "masterSizeCode" : "1.5Y",
    "GC" : "20",
    "DC" : "20"
  }, {
    "productCode" : "AA0714-001",
    "masterSizeCode" : "11C",
    "GC" : "20",
    "DC" : "20"
  }, {
    "productCode" : "BB0204-100",
    "masterSizeCode" : "XL",
    "GC" : "01",
    "DC" : "30"
  }, {
    "productCode" : "BB0204-100",
    "masterSizeCode" : "S",
    "GC" : "01",
    "DC" : "30"
  } ]
}

我的规格:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "product": {
          "productSizes": {
            "*": "Data[]"
          },
          "genderAgeCode": "Data[&2].GC",
          "divisionCode": "Data[&2].DC"
        }
      }
    }
  }
]

我的输出

{
  "Data" : [ {
    "productCode" : "AA0714-001",
    "masterSizeCode" : "1.5Y",
    "GC" : "20",
    "DC" : "20"
  }, {
    "productCode" : "AA0714-001",
    "masterSizeCode" : "11C",
    "GC" : "01",
    "DC" : "30"
  }, {
    "productCode" : "BB0204-100",
    "masterSizeCode" : "XL"
  }, {
    "productCode" : "BB0204-100",
    "masterSizeCode" : "S"
  } ]
}

不太确定在这种情况下如何建立分组。

您需要遍历所有属性的 productSizes 数组的索引,以便重复尽可能多的元素数组有。所以,使用这个规范

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "product": {
          "productSizes": {
            "*": {
              "@": "&[&4]",
              "@(2,genderAgeCode)": "&[&4].GC",
              "@(2,divisionCode)": "&[&4].DC"
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "Data"
      }
    }
  }
]

其中[&4]表示向后遍历四次打开花括号({)以到达并抓取最外层索引(第一个*字符在规范部分 ) 和 & 中根据索引(01)分隔值,以便在 [= 中区分它们17=] 替换