JOLT Spec 合并多个级别的嵌套数组并读取子项下的父键值

问题描述

我正在努力获得所需的输出。有人能帮我找到正确的解决方案吗。

我尝试了不同的规格并注意到有效。在相应数组下添加父 ID 时遇到问题。

在这里帮忙。

输入 JSON:

{
  "device": {
    "id": "336 A6","shelves": [
      {
        "shelfID": "1","slots": [
          {
            "slotID": "1","cards": {
              "cardID": "1","subSlots": [
                {
                  "slotID": "mda-1","cards": {
                    "cardID": "1/1","ports": [
                      {
                        "portID": "1/1/1","adminStatus": "up"
                      },{
                        "portID": "1/1/2","adminStatus": "up"
                      }
                    ]
                  }
                },{
                  "slotID": "mda-2","cards": {
                    "cardID": "2/1","ports": [
                      {
                        "portID": "2/1/1",{
                        "portID": "2/1/2","adminStatus": "up"
                      }
                    ]
                  }
                }
              ]
            }
          },{
            "slotID": "11","cards": {
              "cardID": "11","ports": [
                {
                  "portID": "11/1/1","adminStatus": "up"
                },{
                  "portID": "11/1/2","adminStatus": "up"
                }
              ]
            }
          }
        ]
      },{
        "shelfID": "esat-1","ports": [
          {
            "portID": "esat-1/1/1","adminStatus": "down"
          },{
            "portID": "esat-1/1/2","adminStatus": "up"
          }
        ]
      }
    ]
  }
}

以下是我尝试过的 JOLT 规范

[
  {
    "operation": "shift","spec": {
      "device": {
        "shelves": {
          "*": {
            "ports": {
              "*": "physicalResource.ports[&2]","@(1,shelfID)": "physicalResource.ports[&2].parentId"
            },"slots": {
              "*": {
                "cards": {
                  "ports": {
                    "*": "physicalResource.ports[&5]",cardID)": "physicalResource.ports[&5].parentId"
                  },"subSlots": {
                    "*": {
                      "cards": {
                        "ports": {
                          "*": "physicalResource.ports[&8]",cardID)": "physicalResource.ports[&8].parentId"
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
]

预期输出

{
  "physicalResource": {
    "ports": [
      {
        "id": "1/1/1","parentId": "1/1","state": "up"
      },{
        "id": "1/1/2",{
        "id": "2/1/1","parentId": "2/1",{
        "id": "2/1/2",{
        "id": "11/1/1","parentId": "11/1",{
        "id": "11/1/2",{
        "id": "esat-1/1/1","parentSubCardId": "esat-1",{
        "id": "esat-1/1/2","state": "up"
      }
    ]
  }
}

谢谢。

解决方法

这可以分两班完成

  1. 第一个班次使用数组索引和类型创建组。
  2. 第二个班次将这些组转换成一个数组
[
  {
    "operation": "shift","spec": {
      "device": {
        "shelves": {
          "*": {
            "ports": {
              "*": {
                "@(2,shelfID)": "ports_&3_&1.parentSubCardId","portID": "ports_&3_&1.id","adminStatus": "ports_&3_&1.state"
              }
            },"slots": {
              "*": {
                "cards": {
                  "ports": {
                    "*": {
                      "@(2,cardID)": "slots_cards_&4_&1.parentId","portID": "slots_cards_&4_&1.id","adminStatus": "slots_cards_&4_&1.state"
                    }
                  },"subSlots": {
                    "*": {
                      "cards": {
                        "ports": {
                          "*": {
                            "@(2,cardID)": "sub_slots_&4_&1.parentId","portID": "sub_slots_&4_&1.id","adminStatus": "sub_slots_&4_&1.state"
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },{
    "operation": "shift","spec": {
      "*": "physicalResource.ports[]"
    }
  }
]

我建议在没有第二个班次的情况下运行它,看看发生了什么。