Search code examples
arraysjsonapache-nifijolt

Remove array from JSON when it appears


I receive a JSON and sometimes, not always, I can receive an array in "resources" instead of a single value. I need to always transform this to a single value, keeping only the first one.

{
"result": {
"data": [
{
"postman": "12345",
"recordCase": "12",
"TypOfClient": "Small",
"active": "Yes",
"validFrom": "2020-09-03",
"activeGrades": [
{
"resources": "100",
"accounts": "1234551"
}
],
"validTo": "4712-12-31"
},
{
"postman": "12345",
"recordCase": "12",
"TypOfClient": "Small",
"active": "Yes",
"validFrom": "2020-09-03",
"activeGrades": [
{
"resources": ["100", "200", "500"],
"accounts": "1234551"
}
],
"validTo": "4712-12-31"
}
],
"lastPage": true
},
"status": "Finished"
}

desire result

{
"result": {
"data": [
{
"postman": "12345",
"recordCase": "12",
"TypOfClient": "Small",
"active": "Yes",
"validFrom": "2020-09-03",
"activeGrades": [
{
"resources": "100",
"accounts": "1234551"
}
],
"validTo": "2026-12-31"
},
{
"postman": "224499",
"recordCase": "13",
"TypOfClient": "Small",
"active": "Yes",
"validFrom": "2020-09-11",
"activeGrades": [
{
"resources": "100",
"accounts": "1234551"
}
],
"validTo": "2025-12-31"
}
],
"lastPage": true
},
"status": "Finished"
}

Is that possible? I tried a couple of things but it always erase the entire line or keeps all the values.


Solution

  • You can use a cardinality operation in this case.

    "resources": "ONE" with cardinality means to pick the first element of array and create a single object out of it.

    [
      {
        "operation": "cardinality",
        "spec": {
          "result": {
            "data": {
              "*": {
                "activeGrades": {
                  "*": {
                    "resources": "ONE"
                  }
                }
              }
            }
          }
        }
      }
    ]
    

    Hope this helps you!