Reputation: 114
In Flowgear, I have a list [2,3,4,5,6] and I need 1 to be added for every element. Is there a way to achieve this using a quick map/Formatter node?
Upvotes: 0
Views: 66
Reputation: 61
The list you provided is not valid json
, which means it would be hard to achieve this in one Quickmap.
I did manage to achieve what you were asking for with the below method:
The first Formatter is just there to pass in the list
you provided in your post. This can be any other node that can pass this data.
The second formatter is to pass the list
into a valid json
body so that we can transform the original data for math operations. The json
body I am using can be seen in the screenshot below. {List}
is my variable I am using to pass your list into the second Formatter.
For my first Quickmap node, I just created a very simple json
body that is able to accept a split of the values into their own elements. I have also mapped the Numbers
field from my source payload to the lowest container as well as the Number field so that the Quickmap knows we are working with an array of items. This is also where you will add 1 to your numbers.
In my second quickmap, I have the same source and target structure from the previous quickmap and I mapped it as follows.
Once the above is completed, I pass the list called Numbers into a formatter with the following expression: root.Doc.Numbers
. It is to be noted that Flowgear will by default want to pass out the item with index 0, like this: root.Doc.Numbers[0]
. You will have to click in the expression box and remove the [0]
part.
At this point, the formatter will try to identify the data type of the numbers in the list and from my tests, it looks like it identifies it as float
, so I needed to use a Replace
node to get rid of the ".0".
The alternative to the above would simply be to iterate over the list with a For Each
node, insert the value into some sort of useable xml
or json
document (as we did above) and then passing them into a Quickmap that adds the 1 to your numbers and then using the String Builder
node to append
the value after the Quickmap with a comma. You can hook up a another String Builder
node to the Finished
leg of the For Each
to then read
what was appended when the For Each
completes. After reading the data, you should have something like this: 2,3,4,5,6,
. The final step would be to wrap this value in square brackets and to replace ,]
with ]
.
Upvotes: 1