Search code examples
jsonapache-nifijolt

how to store Name and nameId in an Map in Jolt spec?


I want to store Name and nameId together in a map or in any key:value form in array or whatever so that I can use that later in my executeScript.

My condition is if Name match the incoming data then reasonId need to store or acccess. So I need to store this Name and nameId in such a way that I can easily use it later.

[
  {
    "Name": "Ivor",
    "Specialty": [
      {
        "nameId": "554",
        "specialtyId": "20"
      }
    ]
  },
  {
    "Name": "Philip",
    "Specialty": [
      {
        "nameId": "524",
        "specialtyId": "70"
      }
    ]
  },
  {
    "Name": "Alex",
    "Specialty": [
      {
        "nameId": "594",
        "specialtyId": "90"
      }
    ]
  },
{
    "Name": "Adam",
    "Specialty": [
      {
        "nameId": "504",
        "specialtyId": "10"
      }
    ]
  }]

Expected output json as :

{
  "Name": {
    "Ivor": [
      "554"
    ],
    "Philip": [
      "524"
    ],
    "Alex": [
      "594"
    ],
    "Adam": [
      "504"
    ]
  }
}

Solution

  • You can use such a shift transformation spec within a JoltTransformJSON processor :

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "@Specialty[0].nameId": "Name.@1,Name[]" 
                           // the outermost wrapper is Name 
                           // the value of the Name is the next level wrapper through use of .@1,Name
                           // the innermost "nameId" : <itsvalue> match by .nameId : .nameId
          }
        }
      }
    ]
    

    or the following one as another alternative :

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "Specialty": {
              "*": {
                "nameId": "Name.@3,Name[]" // need to traverse one `:` + two `{`  = three levels in order to reach the level of the "Name"
              }
            }
          }
        }
      }
    ]