1
votes

I'm trying to use UpdateRecord 1.9.0 processor to modify a JSON but it does not replace the values as I want.

this is the source message

{
    "type": "A",
    "ids": [{
            "id": "1",
            "value": "abc"
        }, {
            "id": "2",
            "value": "def"
        }, {
            "id": "3",
            "value": "ghi"
        }
    ]
}

and the wanted output

{
    "ids": [{
            "userId": "1",
        }, {
            "userId": "2",
        }, {
            "userId": "3",
        }
    ]
}

I have configured the processor as follows processor config

Reader: reader

Schema registry: schema

writer: writer

And it works, the output is a JSON without the field 'type' and the ids have the field 'userId' instead 'id' and 'value'.

To fill the value of userId, I defined the replace strategy and the property to replace: strategy

But the output is wrong. The userId is always filled with the id of the last element in the array:

{
    "ids": [{
            "userId": "3"
        }, {
            "userId": "3"
        }, {
            "userId": "3"
        }
    ]
}

I think the value of the expression is ok because if I try to replace only one record it works fine (/ids[0]/userId, ..id)

Nifi docs has a really similar example (example 3): https://nifi.apache.org/docs/nifi-docs/components/org.apache.nifi/nifi-standard-nar/1.7.1/org.apache.nifi.processors.standard.UpdateRecord/additionalDetails.html

But it does not work for me.

What am I doing wrong?

thanks

1

1 Answers

3
votes

Finally I have used JoltJSONTransform processor instead UpdateRecord

JoltJSONTransform

template:

[
    {
        "operation": "shift",
        "spec": {
            "ids":{
                "*":{
                    "id": "ids[&1].userId"
                }
            }
        }
    }
]

Easier than UpdateRecord