2
votes

i have a attribute (that produced by a REST service and catched by invokeHTTP processor) in JSON format like this:

test => {"key":"value"}

and then i want to put it in flows JSON content using JOLT processor, my content is something like this:

{
    "id": 123,
    "user": "foo"
}

and my JOLT specification is this:

[{
    "operation": "default",
    "spec": {
        "interest": "${test}"
    }
}]

the problem is here that, in JOLT advanced window with test attribute nifi cannot put json object and shown this error:

"Error occurred during transformation"

and when run processor this detailed error is become alerted:

"unable to unmarshal json to an object"


my desired result is this:

{
    "id": 123,
    "user": "foo",
    "interest": {"key":"value"}
}

another possible question is... am i choose right solution to overcome this situation? i have a flow that contain JSON content and with invokeHTTP that sent to a REST service and then response store in attribute and then with JOLT processor i combine those together (previous JSON and new JSON in attribute)

2
it's worked with escaping JSON: {\"key\":\"value\"} but when insert in JSON that have stringed because of double quotation (" ") { "id": 123, "user": "foo", "interest": "{\"key\":\"value\"}" } any idea? - meh

2 Answers

2
votes

As you are having test attribute associated with the flowfile

  • Try with ReplaceText processor instead of JoltTransformJson

ReplaceText Configs:

Search Value

}

Replacement Value

,"interest": ${test}}

Character Set UTF-8 Maximum Buffer Size

1 MB

Replacement Strategy

Literal Replace

Evaluation Mode

Entire text

enter image description here Input:

{"id": 123,"user": "foo"}

Output:

{"id": 123,"user": "foo","interest": {"key":"value"}}
1
votes

I also encountered the same issue in Nifi 1.6.0, the Advanced editor converted the input without issue, but automatic processing failed. The error I received:

due to com.bazaarvoice.jolt.exception.JsonUnmarshalException: Unable to unmarshal JSON to an Object.: Unable to unmarshal JSON to an Object

It turns out the problem were some unescaped Unicode characters in the input that were not visible and had been lost on copy/paste when debugging. To fix it and also find the error I added ":escapeJson()" for each of the attributes. For example:

{"key":${myvalue:escapeJson()}}

Looking at the output flow file I was then able to determine the attribute was causing the problem.

The same solution I found was also posted here: https://community.cloudera.com/t5/Support-Questions/JoltTransformation-unable-to-marshal-json/m-p/230679/highlight/true#M192526