Home > Back-end >  JOLT add key to array of object
JOLT add key to array of object

Time:01-20

For each array object, get the id and assign it as a key to the array object. I am trying to use the JOLT processor in NIFI. Any help would be much appreciated!

Input:

{
  "list": [
    {
      "catalog": {
        "id": "0981",
        "Desc": "Chess toy"
      },
      "quantity": 10,
      "price": 10.5
    },
    {
      "catalog": {
        "id": "01234",
        "Desc": "Water bottle"
      },
      "quantity": 5,
      "price": 5.4
    }
  ]
}

Expected output:

{
  "list": [
    {
      "0981": {
        "catalog": {
          "id": "0981",
          "Desc": "Chess toy"
        },
        "quantity": 10,
        "price": 10.5
      },
      "01234": {
        "catalog": {
          "id": "01234",
          "Desc": "Water bottle"
        },
        "quantity": 5,
        "price": 5.4
      }
    }
  ]
}

CodePudding user response:

You can use this shift transformation spec

[
  {
    "operation": "shift",
    "spec": {
      "list": {
        "*": {
          "*": {
            "@": "&3[#4].@1,id.&",
            "@1,quantity": "&3[#4].@1,id.quantity",
            "@1,price": "&3[#4].@1,id.price"
          }
        }
      }
    }
  }
]

where keys come from the identifier @1,id, &3 represents going tree three levels up to get the literal list and [#4] will bring them in array type manner.

The following will do it all dynamically

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "cat*": {
            "@": "&3[#4].@(1,id).&1"
          },
          "*": "&2[#3].@(1,catalog.id).&"
        }
      }
    }
  }
]

CodePudding user response:

You can use this spec:

[
  {
    "operation": "shift",
    "spec": {
      "*": { // list
        "*": { // 0, 1
          "catalog": {
            "@": "&3.@(1,id).&1"
          },
          "*": "&2.@(1,catalog.id).&"
        }
      }
    }
  }
]
  • Related