Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Nifi jolt transaformation json array for each element

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Can you explain me how I can produce such output ?

>Solution :

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 http://jolt-demo.appspot.com/ is

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading