I've a JSON:
[
{
"id": 1,
"status": 0
},
{
"id": 2,
"status": 0
},
{
"id": 3,
"status": 1
}
]
And I've an attribute task-ids
. It's a string with comma separated values. For example it can be: 500, 501, 502
, or 765, 780, 790
or 10, 11, 12
etc. Numbers can be completely random, but their order and amount matches with order and amount of json objects. I mean first element from string is always should go to the first object in json, second to second, etc. For the first case 500
should go to object with id
= 1, 501 to 2, 502 to 3. Order in this string equals to order of objects. So I expect this:
[
{
"id": 1,
"status": 0,
"taskId": 500
},
{
"id": 2,
"status": 0,
"taskId": 501
},
{
"id": 3,
"status": 1,
"taskId": 502
}
]
My task is: split string from an attribute and fill source JSON by these values from string. Order is maintained. First element from an attribute should go to first element of JSON, etc.
I wrote simple Groovy script for this task, but maybe there is another option to solve this task (without using scripts)?
def numbersString = flowFile.getAttribute("task-ids");
def numbersArray = numbersString.split(',').collect { it as Integer }
for (int i = 0; i < numbersArray.size(); i++) {
jsonArray[i].taskId = numbersArray[i]
}
You might use intSum function within a modify transformation such as
[
{
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"taskId": "=intSum(@(1,id),499)"
}
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is :