Search code examples
jsonapache-nifijolt

jolt transformation - array array inside apache nifi


my attempts

Input :

{
  "results": [
    {
      "group": {
        "queueId": "fila1",
        "mediaType": "message1"
      },
      "data": [
        {
          "metric": "oInteracting1",
          "stats": {
            "count": 1
          },
          "truncated": false
        },
        {
          "metric": "oWaiting1",
          "stats": {
            "count": 0
          }
        }
      ]
    },
    {
      "group": {
        "queueId": "fila2",
        "mediaType": "message2"
      },
      "data": [
        {
          "metric": "oInteracting2",
          "stats": {
            "count": 8
          },
          "truncated": false
        },
        {
          "metric": "oWaiting2",
          "stats": {
            "count": 0
          }
        }
      ]
    },
    {
      "group": {
        "queueId": "fila3",
        "mediaType": "message3"
      },
      "data": [
        {
          "metric": "oInteracting3",
          "stats": {
            "count": 1
          },
          "truncated": false
        },
        {
          "metric": "oWaiting3",
          "stats": {
            "count": 0
          }
        }
      ]
    }
  ]
}

Unsuccessful attempt :

[
  {
    "operation": "shift",
    "spec": {
      "results": {
        "*": {
          "@group.queueId": "[&].queueid",
          "@group.mediaType": "[&].mediatype",
          "data": {
            "*": {
              "@metric": "[&3].metric1",
              "@stats.count": "[&3].metric1_count"
            }
          }
        }
      }
    }
  }
]

Unsuccessful Output :

[
  {
    "queueid": "fila1",
    "mediatype": "message1",
    "metric1": [
      "oInteracting1",
      "oWaiting1"
    ],
    "metric1_count": [
      1,
      0
    ]
  },
  {
    "queueid": "fila2",
    "mediatype": "message2",
    "metric1": [
      "oInteracting2",
      "oWaiting2"
    ],
    "metric1_count": [
      8,
      0
    ]
  },
  {
    "queueid": "fila3",
    "mediatype": "message3",
    "metric1": [
      "oInteracting3",
      "oWaiting3"
    ],
    "metric1_count": [
      1,
      0
    ]
  }
]

Expected output :

[
  {
    "queueid": "fila1",
    "mediatype": "message1",
    "oInteracting1": 1,
    "oWaiting1": 0
  },
  {
    "queueid": "fila2",
    "mediatype": "message2",
    "oInteracting1": 8,
    "oWaiting1": 0
  },
  {
    "queueid": "fila3",
    "mediatype": "message3",
    "oInteracting3": 1,
    "oWaiting3": 0
  }
]

Also trying to add the condition

"data": {"\*": { "stats.count": "metric"}} 

but it didn't work

Thanks


Solution

  • Just recursively match within the stats object such as

    [
      {
        "operation": "shift",
        "spec": {
          "results": {
            "*": {
              "@group.queueId": "[&].queueid",
              "@group.mediaType": "[&].mediatype",
              "data": {
                "*": {
                  "stats": {
                    "@count": "[&4].@2,metric" // traverses : and { ( 2 levels ) to reach upto its original level within the JSON
                  }
                }
              }
            }
          }
        }
      }
    ]
    

    the demo on the site http://jolt-demo.appspot.com/ is

    enter image description here