JOLT Spec with merging Nested arrays from multiple levels and read parent key value under child

Viewed 198

I'm struggling to get the desired out put. can someone help me to find the right solution.

I tried with different specifications and noting worked. I'm facing issues when adding the parent id under respective array.

Please help here.

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

Below is the JOLT spec I tried

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

Expected Output:

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

Thanks.

1 Answers

This can be done in two shifts

  1. First shift creates groups using array indexes and types.
  2. Second shift coverts those groups into an array
[
  {
    "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[]"
    }
  }
]

I'd suggest running it without the second shift to see whats going on.

Related