json 如何在Jolt变换中用嵌套数组变换连接对象

x7yiwoj4  于 2023-10-21  发布在  其他
关注(0)|答案(1)|浏览(113)

输入内容

[
  {
    "MAIN": {
      "SCALE": "N",
      "TAX": "INV",
      "DETAIL_GROUP": [
        {
          "ORDER_GROUP": "LD6",
          "CENTRAL_TP": [
            {
              "LEG_DETAILS": [
                {
                  "DETAIL_LINE": [
                    {
                      "ORDER_LINE": "L6",
                      "DETAILS": [
                        {
                          "AMOUNT": 1,
                          "QTY": 1,
                          "REGION": "EMEA"
                        }
                      ]
                    },
                    {
                      "DETAILS": [
                        {
                          "AMOUNT": 2,
                          "QTY": 2,
                          "REGION": "EMEA"
                        }
                      ]
                    }
                  ]
                },
                {
                  "DETAIL_LINE": [
                    {
                      "DETAILS": [
                        {
                          "AMOUNT": 3,
                          "QTY": 3,
                          "REGION": "EMEA"
                        }
                      ]
                    },
                    {
                      "DETAILS": [
                        {
                          "AMOUNT": 4,
                          "QTY": 4,
                          "REGION": "EMEA"
                        }
                      ]
                    }
                  ]
                },
                {
                  "DETAIL_LINE": [
                    {
                      "DETAILS": [
                        {
                          "AMOUNT": 5,
                          "QTY": 5,
                          "REGION": "EMEA"
                        }
                      ]
                    }
                  ]
                },
                {
                  "DETAIL_LINE": [
                    {
                      "DETAILS": [
                        {
                          "AMOUNT": 6,
                          "QTY": 6,
                          "REGION": "EMEA"
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    }
  }
]

规格

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "@MAIN": {
          "@DETAIL_GROUP": {
            "*": {
              "@ORDER_GROUP": "detail[&2].orderGrouop",
              "@CENTRAL_TP": {
                "*": {
                  "@LEG_DETAILS": {
                    "*": {
                      "@DETAIL_LINE": {
                        "*": {
                          "@ORDER_LINE": "detail[&2].orderLine",
                          "@DETAILS": {
                            "*": {
                              "@AMOUNT": "detail[&2].itemInfo.adjustment",
                              "@QTY": "detail[&2].itemInfo.quantity",
                              "REGION": {
                                "#region": "detail[&4].itemInfo.additionalInfo[0].property",
                                "@": "detail[&4].itemInfo.additionalInfo[0].value"
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
]

输出

{
  "detail": [
    {
      "orderGrouop": "LD6",
      "orderLine": "L6",
      "itemInfo": {
        "adjustment": [
          1,
          3,
          5,
          6
        ],
        "quantity": [
          1,
          3,
          5,
          6
        ],
        "additionalInfo": [
          {
            "property": [
              "region",
              "region",
              "region",
              "region"
            ],
            "value": [
              "EMEA",
              "EMEA",
              "EMEA",
              "EMEA"
            ]
          }
        ]
      }
    },
    {
      "itemInfo": {
        "adjustment": [
          2,
          4
        ],
        "quantity": [
          2,
          4
        ],
        "additionalInfo": [
          {
            "property": [
              "region",
              "region"
            ],
            "value": [
              "EMEA",
              "EMEA"
            ]
          }
        ]
      }
    }
  ]
}

预期

{
  "detail": [
    {
      "orderGrouop": "LD6",
      "orderLine": "L6",
      "itemInfo": {
        "adjustment": 1,
        "quantity": 1,
        "additionalInfo": [
          {
            "property": "region",
            "value": "EMEA"
          }
        ]
      }
    },
    {
      "orderGrouop": "LD6",
      "orderLine": "L6",
      "itemInfo": {
        "adjustment": 2,
        "quantity": 2,
        "additionalInfo": [
          {
            "property": "region",
            "value": "EMEA"
          }
        ]
      }
    },
    {
      "orderGrouop": "LD6",
      "orderLine": "L6",
      "itemInfo": {
        "adjustment": 3,
        "quantity": 3,
        "additionalInfo": [
          {
            "property": "region",
            "value": "EMEA"
          }
        ]
      }
    },
    {
      "itemInfo": {
        "adjustment": 4,
        "quantity": 4,
        "additionalInfo": [
          {
            "property": "region",
            "value": "EMEA"
          }
        ]
      }
    },
    {
      "itemInfo": {
        "adjustment": 5,
        "quantity": 5,
        "additionalInfo": [
          {
            "property": "region",
            "value": "EMEA"
          }
        ]
      }
    },
    {
      "itemInfo": {
        "adjustment": 6,
        "quantity": 6,
        "additionalInfo": [
          {
            "property": "region",
            "value": "EMEA"
          }
        ]
      }
    }
  ]
}
fcipmucu

fcipmucu1#

它将添加更多的标识符节点(在本例中,我使用了**&5_&3&6_&4来表示相对元素的深度)。顺便说一句,除非是路径定义,否则不需要在键前面加上@**前缀。因此,您可以使用以下转换:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "@MAIN.DETAIL_GROUP": {
          "*": {
            "CENTRAL_TP": {
              "*": {
                "LEG_DETAILS": {
                  "*": {
                    "DETAIL_LINE": {
                      "*": {
                        "DETAILS": {
                          "*": {
                            "@8,ORDER_GROUP": "&5_&3.orderGroup",
                            "@2,ORDER_LINE": "&5_&3.orderLine",
                            "AMOUNT": "&5_&3.itemInfo.adjustment",
                            "QTY": "&5_&3.itemInfo.quantity",
                            "REGION": {
                              "#region": "&6_&4.itemInfo.additionalInfo[0].property",
                              "@": "&6_&4.itemInfo.additionalInfo[0].value"
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  { // nest whole content within the "detail" array 
    "operation": "shift",
    "spec": {
      "*": "detail[]"
    }
  }
]

相关问题