Modifying JSON from flowfile attribute using JoltTransformJSON

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Modifying JSON from flowfile attribute using JoltTransformJSON

bsavard
Hi,

NiFi noobie here, so go easy on me :)

I have a flowfile attribute (let's call it "Data") that contains JSON.  I
need to remove certain pairs from that JSON and then use the resulting JSON
as the body of a POST to a URL.

The flow file attribute "Data" contains the following JSON:

[{
        "one": "a",
        "two": "b",
        "three": "c",
        "four": "d"
}, {
        "one": "e",
        "two": "f",
        "three": "g",
        "four": "h"
}]

I want to remove elements "two" and "four", resulting in this JSON:

[{
        "one": "a",
        "three": "c"
}, {
        "one": "e",
        "three": "g"
}]

I'm trying to use the JoltTransformJSON processor.
My Jolt Specification is this:

[{
        "operation": "remove",
        "spec": {
                "*": {
                        "two": "",
                        "four": ""
                }
        }
}]

When I test it via the Advanced button UI, it works.
But when I run the flow, I get an error:
"com.bazaarvoice.jolt.exception.JsonUnmarshalException: Unable to unmarshal
JSON to an Object"

How exactly should I be referring to the flow file attribute in the spec?
I've even tried a simple spec like this (where uuid is a flowfile attribute
that contains "foo":
[{
        "operation": "default",
        "spec": {
                "*": {
                        "five": "${uuid}"
                }
        }
}]
Again, this works when I test it via the Advanced button UI, but I get the
same error when I run the actual flow.  It feels like there's some basic
thing I'm missing.
TIA for any pointers!



--
Sent from: http://apache-nifi-users-list.2361937.n4.nabble.com/
Reply | Threaded
Open this post in threaded view
|

Re: Modifying JSON from flowfile attribute using JoltTransformJSON

Jairo Henao
Hi Bob,

The JoltTransformJSON processor operates on the content of the flowfile. You can use attributes in your Jolt specification or even build Nifi expressions, but you must ensure that the content of your flowfile contains the JSON you are going to transform.

You could use a ReplaceText and thus pass the value of the attribute to the content of the flowfile. So then apply the JoltTransform. Please note that this will cause the entire content of your flowfile to be replaced.



On Wed, Jul 8, 2020 at 3:38 PM bsavard <[hidden email]> wrote:
Hi,

NiFi noobie here, so go easy on me :)

I have a flowfile attribute (let's call it "Data") that contains JSON.  I
need to remove certain pairs from that JSON and then use the resulting JSON
as the body of a POST to a URL.

The flow file attribute "Data" contains the following JSON:

[{
        "one": "a",
        "two": "b",
        "three": "c",
        "four": "d"
}, {
        "one": "e",
        "two": "f",
        "three": "g",
        "four": "h"
}]

I want to remove elements "two" and "four", resulting in this JSON:

[{
        "one": "a",
        "three": "c"
}, {
        "one": "e",
        "three": "g"
}]

I'm trying to use the JoltTransformJSON processor.
My Jolt Specification is this:

[{
        "operation": "remove",
        "spec": {
                "*": {
                        "two": "",
                        "four": ""
                }
        }
}]

When I test it via the Advanced button UI, it works.
But when I run the flow, I get an error:
"com.bazaarvoice.jolt.exception.JsonUnmarshalException: Unable to unmarshal
JSON to an Object"

How exactly should I be referring to the flow file attribute in the spec?
I've even tried a simple spec like this (where uuid is a flowfile attribute
that contains "foo":
[{
        "operation": "default",
        "spec": {
                "*": {
                        "five": "${uuid}"
                }
        }
}]
Again, this works when I test it via the Advanced button UI, but I get the
same error when I run the actual flow.  It feels like there's some basic
thing I'm missing.
TIA for any pointers!



--
Sent from: http://apache-nifi-users-list.2361937.n4.nabble.com/


--
Saludos

Jairo Henao

Chat Skype: jairo.henao.05

Reply | Threaded
Open this post in threaded view
|

Re: Modifying JSON from flowfile attribute using JoltTransformJSON

bsavard
Thank you that was the piece I was missing and it now works.




--
Sent from: http://apache-nifi-users-list.2361937.n4.nabble.com/