I try to get friends with the node transformer so I made a small test transformer for that using basically two different input objects: a simple array {“a”:[1,2,3]} and an object array {“a”:[{“b”:1},{“b”:2},{“b”:3}]}.
Now since my intention is to use HTTP transformer to make request on each item in the array, I use a merge transformer which should write the result to a separate object, “result”:.
Using a value setter inside the merge transformer just to test how things go. Expected to get “result”:[{“b”:“processed 1”},{“b”:“processed 2”},{“b”:“processed 3”}].
Anyone can help me out on this? I have seen the videos that I found on this, but somehow haven’t figured this out - here’s my test node transformer.
export_20230518080706.ndjson (2.3 KB)
Hi @kjetil
We checked the exported configurations and found out that you used the “Node transformer” prototype. Unfortunately, the prototype doesn’t work as expected at the moment. You should use an entity transformer called “Node, transform nodes”, which is in the same level as “Data, transform data using mappers and conditions”.
In addition, “Node, transform nodes” can only be used to loop through an array of objects or an associated array of objects, such as below.
// Array of objects
{
"options": [
{
"id": "123"
},
{
"id": "123"
},
{
"id": "123"
}
]
}
// Associated array of objects
{
"options": [
"123": {
"id": "123"
},
"456": {
"id": "123"
},
"789": {
"id": "123"
}
]
}
It cannot loop through an array such as below.
{
"options": [
"123",
"456",
"789"
]
}
If you need to loop to the above array using the “Node, transform nodes” entity transformer, you can map the options
property into an array of objects using the below “Move using a pattern” transformer.
Inside the node transformer, you cannot read or write any properties outside the array it is looping. For example, if you use the “Node, transform nodes” to loop through the options
array, you will not be able to read or write any properties outside of the options
array to write or create a new property called result
.
For instance, you merge an HTTP transformer result to a property named result
, so each object has a result
property such as below.
{
"id": "1",
"options": [
{
"id": "123",
"result": {
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
}
},
{
"id": "345",
"result": {
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
}
},
{
"id": "456",
"result": {
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
}
}
]
}
You can then copy the result from each object to a property called result
in the root of your entity using the below transformer.
Please give it a try and let us know if you have any further questions.
Thank you for this thorough explanation, it helps a lot! In my case, since the node transformer does not read from root, I need to copy root data (a product ID) into each of those nodes before iterating over them. Would you have advice on how to do that too?
Something like $.options[*].root_id = $.ID
.
Edit: I found the answer to this - found the data transformer named “Recursively copy values to children” which does the trick.
You are correct that you can use Recursively copy values to children to copy a value to all objects in an array.