Home > Back-end >  Nifi jolt transformation json array for each element
Nifi jolt transformation json array for each element

Time:08-24

I have a following input in Nifi Jolt Specification processor :

{
  "transaction_id": 53279810162,
  "bets": [
    {
      "event_name": "Mawkhar Sc - Rangdajied United FC (live)",
      "match_start": "1660905000000",
      "game_name": "Handicap ? £",
      "outcome_name": "2 (3:0)"
    },
    {
      "event_name": "University Azzurri Fc - Hellenic Athletic Club (live)",
      "match_start": "1660905000000",
      "game_name": "Handicap ? £",
      "outcome_name": "2 (3:0)"
    }
  ],
  "user_id": 1009425,
  "bet_type": "Multi"
}

and from this input I want to get such output :

{
  "transaction_id": 53279810162,
  "bets": [
    {
      "event_name": "Mawkhar Sc - Rangdajied United FC (live)",
      "match_start": "1660905000000",
      "outcomes": [
        {
          "outcome_name": "2 (3:0)",
          "game_name": "Handicap ? £"
        }
      ]
    },
    {
      "event_name": "University Azzurri Fc - Hellenic Athletic Club (live)",
      "match_start": "1660905000000",
      "outcomes": [
        {
          "outcome_name": "2 (3:0)",
          "game_name": "Handicap ? £"
        }
      ]
    }
  ],
  "user_id": 1009425,
  "bet_type": "Multi"
}

I need for each "bets" create new array with the name "outcomes", where placed game_name and outcome_name.

Can you explain me how I can produce such output ?

CodePudding user response:

You can use such a single shift transformation

[
  {
    "operation": "shift",
    "spec": {
      "*": "&", // the "else" case(the attributes other than "bets")
      "bets": {
        "*": {
          "*": "&2[&1].&", // &2 represents "bets", and [&1] indexes of it
          "ou*|ga*": "&2[&1].outcomes[#].&" // # replicates the indexes of the newly created array "outcomes", and & substitues the values of the current attributes starting with "ou" OR "ga"
        }
      }
    }
  }
]

the demo on the site enter image description here

  • Related