Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

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!