Support Questions

Find answers, ask questions, and share your expertise

how to replace first element of an array using jolt

avatar
Expert Contributor

Hi All,

 

I would like to replace first element of an array value  i.e would like to replace with first net val with nifi attribute

 

input

{
"iH": [
{
"DN": "711449",
"iLineList": [
{
"DNnumber": "711449",
"DNLineNumber": 2
},
{
"DNNumber": "711449",
"DNLineNumber": 3
}
],
"netval": "22.09"
},
{
"DN": "711450",
"iLineList": [
{
"DNnumber": "711450",
"DNLineNumber": 2
},
{
"DNNumber": "711450",
"DNLineNumber": 3
}
],
"netval": "11.09"
}
]
}

defined SPEC like :

[
{
"operation": "shift",
"spec": {
"*": {
"0": {
"netVal": "iH[&1].netVal[0]",
"*": "iH[&1].&"
},
"*": "&1"
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"*": {
// "invoiceHeader": {
"0": {

"netVal": "${netVal}"
}
// }
}
}
}
]

Expected output:
{
"iH" : [ {
"DN" : "711449",
"iLineList" : [ {
"DNnumber" : "711449",
"DNLineNumber" : 2
}, {
"DNNumber" : "711449",
"DNLineNumber" : 3
} ],
"netval" : "${netval}"
}, {
"DN" : "711450",
"iLineList" : [ {
"DNnumber" : "711450",
"DNLineNumber" : 2
}, {
"DNNumber" : "711450",
"DNLineNumber" : 3
} ],
"netval" : "11.09"
} ]
}

 

1 ACCEPTED SOLUTION

avatar
Super Guru

Hi,

This is interesting . It looks trivial initially but its not that simple 🙂 It took me trial and error to get it to work using modify-overwrite-beta because the syntax is different from the shift spec where you reference array element at n index as "n" while in modify-overwrite-beta its "[0]" ! not sure why the difference.  So this should work:

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "iH": {
        "[0]": {
          "netval": "${netval}"
        }
      }
    }
}
]

You can also do it using shift as follows:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "0": {
          "netval": {
            "#${netval}": "&3[&2].&1"
          },
          "*": "&2[&1].&"
        },
        "*": "&1[&]"
      }
    }
}
]

If that helps please accept solution.

Thanks

 

View solution in original post

3 REPLIES 3

avatar
Super Guru

Hi,

This is interesting . It looks trivial initially but its not that simple 🙂 It took me trial and error to get it to work using modify-overwrite-beta because the syntax is different from the shift spec where you reference array element at n index as "n" while in modify-overwrite-beta its "[0]" ! not sure why the difference.  So this should work:

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "iH": {
        "[0]": {
          "netval": "${netval}"
        }
      }
    }
}
]

You can also do it using shift as follows:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "0": {
          "netval": {
            "#${netval}": "&3[&2].&1"
          },
          "*": "&2[&1].&"
        },
        "*": "&1[&]"
      }
    }
}
]

If that helps please accept solution.

Thanks

 

avatar
Expert Contributor

Thanks a Lot the JOLT Master @SAMSAL , Modify-overwrite beta -logic worked perfectly.

but for Shift logic jolt demo throwing an execption Invalid reference key=#${netval} either blank or doesn't start with correct character=$

avatar
Expert Contributor

@SAMSAL , After i added escaping to $ with double slash it got worked in shift too.

"#\\${netval}": "&3[&2].&1"

 

thanks a lot again