JOLT-将包含嵌套数组的JSON对象数组转换为其各自的数组组

时间:2019-11-07 06:47:19

标签: json jolt

我想将JSON输入文件转换为预期的JSON输出文件:

我的输入文件:

[
  {
    "category": [
      {
        "id": "id1",
        "name": "Connected Home & Housewares1"
      },
      {
        "id": "id2",
        "name": "Housewares1"
      },
      {
        "id": "id3",
        "name": "Household Batteries1"
      },
      {
        "id": "id4",
        "name": "Alkaline Batteries1"
      }
    ]
  },
  {
    "category": [
      {
        "id": "id1",
        "name": "Connected Home & Housewares2"
      },
      {
        "id": "id2",
        "name": "Housewares2"
      },
      {
        "id": "id3",
        "name": "Household Batteries2"
      },
      {
        "id": "id4",
        "name": "Alkaline Batteries2"
      }
    ]
  }
]

(以上只是2条记录的示例,但其中包含约5万条记录)

预期输出:

[
  {
    "childSKUs": [
      {
        "divisionName": "Connected Home & Housewares1",
        "deptName": "Housewares1",
        "className": "Household Batteries1"
      }
    ]
  },
  {
    "childSKUs": [
      {
        "divisionName": "Connected Home & Housewares2",
        "deptName": "Housewares2",
        "className": "Household Batteries2"
      }
    ]
  }
]

我当前的JOLT规范:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "category": {
          "0": {
            "name": "[0].childSKUs[&1].divisionName"
          },
          "1": {
            "name": "[0].childSKUs[&1].deptName"
          },
          "2": {
            "name": "[0].childSKUs[&1].className"
          }
        }
      }
    }
  }
]

但是它只给出以下输出:

[
  {
    "childSKUs": [
      {
        "divisionName": [
          "Connected Home & Housewares1",
          "Connected Home & Housewares2"
        ]
      },
      {
        "deptName": [
          "Housewares1",
          "Housewares2"
        ]
      },
      {
        "className": [
          "Household Batteries1",
          "Household Batteries2"
        ]
      }
    ]
  }
]

我通过玩https://jolt-demo.appspot.com尝试了各种选择,但到目前为止还没有运气。任何帮助表示赞赏。如果您解释一下SPEC是如何完成的,那也很好。

1 个答案:

答案 0 :(得分:0)

好吧,我自己弄清楚了。

感谢@Pokuri在这里的回答,这使我有了解决方案的想法:https://stackoverflow.com/a/55861132/248847

更正上述问题的JOLT SPEC:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "category": {
          "0": {
            "name": "[&3].childSKUs[&1].divisionName"
          },
          "1": {
            "name": "[&3].childSKUs[0].deptName"
          },
          "2": {
            "name": "[&3].childSKUs[0].className"
          }
        }
      }
    }
  }
]