Our Community is getting an upgrade! To get everything ready for the relaunch, we’ll be placing the site in read-only mode starting September 21st.
We really appreciate your understanding while we get things set up behind the scenes. Catch up on all the exciting details about the move here.
Need help or have questions? Drop us a line at [email protected]

Support Questions

Find answers, ask questions, and share your expertise
Announcements
Share your experience with Cloudera on G2 and get a $25 Amazon Gift card.
Hi, I'm CLEO! Something exciting is coming to the Community. Stay Tuned!

NiFi - JOLT set defaults within an array

avatar
Master Collaborator

Hi, I need some help with JOLT-syntax.

JSON-Input

{
  "nummer": "22",
  "id": "177",
  "table": [
    {
      "zn": 1,
      "stfflbisart": 10
    },
    {
      "zn": 2,
      "stfflbisart": 50
    }
  ]
}

JOLT-Specification

[{
    "operation": "shift",
    "spec": {
      "id": "ID",
      "nummer": "Nummer",
      "table": {
        "*": {
          "zn": "ArtikelPreise_Pos.[#2].ZeileNr",
          "stfflbisart": "ArtikelPreise_Pos.[#2].StaffelBis"
        }
      }
    }
}, {
    "operation": "default",
    "spec": {
      "Default_Kopf": "${VAR_KD}"
    }
    }
]

JSON-Output

{
  "ID" : "177",
  "Nummer" : "22",
  "ArtikelPreise_Pos" : [ {
    "ZeileNr" : 1,
    "StaffelBis" : 10
  }, {
    "ZeileNr" : 2,
    "StaffelBis" : 50
  } ],
  "Default_Kopf" : "${DFT_KOPF}"
}


Like in "Default_Kopf" I need to set a default value to each element of the array "ArtikelPreise_Pos" coming from flowfile attribute "DFT_POS".

The result should look like this:


WANTED JSON-Output

{
  "ID" : "177",
  "Nummer" : "22",
  "ArtikelPreise_Pos" : [ {
    "ZeileNr" : 1,
    "StaffelBis" : 10,
    "Default_Pos" : "${DFT_POS}"
  }, {
    "ZeileNr" : 2,
    "StaffelBis" : 50,
    "Default_Pos" : "${DFT_POS}"
  } ],
  "Default_Kopf" : "${VAR_KD}"
}


Tried things like this, but it doesn't work:

[{
    "operation": "shift",
    "spec": {
      "id": "ID",
      "nummer": "Nummer",
      "table": {
        "*": {
          "zn": "ArtikelPreise_Pos.[#2].ZeileNr",
          "stfflbisart": "ArtikelPreise_Pos.[#2].StaffelBis"
        }
      }
    }
}, {
    "operation": "default",
    "spec": {
      "Default_Kopf": "${DFT_KOPF}",
      "ArtikelPreise_Pos": {
        "Default_Pos": "${DFT_POS}"
      }
    }
      }
]


Any help? Thanks.

1 ACCEPTED SOLUTION

avatar
Master Guru

Try with below jolt spec:

[{
    "operation": "shift",
    "spec": {
      "id": "ID",
      "nummer": "Nummer",
      "table": {
        "*": {
          "zn": "ArtikelPreise_Pos.[#2].ZeileNr",
          "stfflbisart": "ArtikelPreise_Pos.[#2].StaffelBis"
        }
      }
    }
}, {
    "operation": "default",
    "spec": {
      "Default_Kopf": "${VAR_KD}",
      "ArtikelPreise_Pos[]": {
        "*": {
          "Default_Kopf": "${DFT_POS}"
        }
      }
    }
    }
]

Output:

{
  "ID" : "177",
  "Nummer" : "22",
  "ArtikelPreise_Pos" : [ {
    "ZeileNr" : 1,
    "StaffelBis" : 10,
    "Default_Kopf" : "${DFT_POS}"
  }, {
    "ZeileNr" : 2,
    "StaffelBis" : 50,
    "Default_Kopf" : "${DFT_POS}"
  } ],
  "Default_Kopf" : "${VAR_KD}"
}

I hope this matches with your expected output.

-

If the answer is helpful to resolve the issue, Login and Click on Accept button below to close this thread.This will help other community users to find answers quickly 🙂

View solution in original post

2 REPLIES 2

avatar
Master Guru

Try with below jolt spec:

[{
    "operation": "shift",
    "spec": {
      "id": "ID",
      "nummer": "Nummer",
      "table": {
        "*": {
          "zn": "ArtikelPreise_Pos.[#2].ZeileNr",
          "stfflbisart": "ArtikelPreise_Pos.[#2].StaffelBis"
        }
      }
    }
}, {
    "operation": "default",
    "spec": {
      "Default_Kopf": "${VAR_KD}",
      "ArtikelPreise_Pos[]": {
        "*": {
          "Default_Kopf": "${DFT_POS}"
        }
      }
    }
    }
]

Output:

{
  "ID" : "177",
  "Nummer" : "22",
  "ArtikelPreise_Pos" : [ {
    "ZeileNr" : 1,
    "StaffelBis" : 10,
    "Default_Kopf" : "${DFT_POS}"
  }, {
    "ZeileNr" : 2,
    "StaffelBis" : 50,
    "Default_Kopf" : "${DFT_POS}"
  } ],
  "Default_Kopf" : "${VAR_KD}"
}

I hope this matches with your expected output.

-

If the answer is helpful to resolve the issue, Login and Click on Accept button below to close this thread.This will help other community users to find answers quickly 🙂

avatar
Master Collaborator

@Shu Oh, so simple... thank you so much!