【问题标题】:JOLT Spec with merging Nested arrays from multiple levels and read parent key value under childJOLT Spec 合并来自多个级别的嵌套数组并读取子项下的父键值
【发布时间】:2021-02-22 18:02:56
【问题描述】:

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

我尝试了不同的规格并注意到有效。在相应数组下添加父 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",
                        "adminStatus": "up"
                      },
                      {
                        "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]",
                    "@(1,cardID)": "physicalResource.ports[&5].parentId"
                  },
                  "subSlots": {
                    "*": {
                      "cards": {
                        "ports": {
                          "*": "physicalResource.ports[&8]",
                          "@(1,cardID)": "physicalResource.ports[&8].parentId"
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
]

预期输出:

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

谢谢。

【问题讨论】:

标签: jolt


【解决方案1】:

这可以分两班完成

  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[]"
    }
  }
]

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

【讨论】:

    猜你喜欢
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 2019-06-06
    • 2017-04-24
    • 2016-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多