Barath
Barath

Reputation: 5283

Apache Nifi: How to convert string (text/plain) to JSON type using Nifi processor?

Please guide me the right component for converting string to json using appropriate Nifi processor component.

Input is a string of content type text/plain

{ productName : "tv", locationName: " chennai"}

Output of EvaluateJsonPath is still the same as I am unable to evaluate json property based on json path due to wrong content type sent as input.

{ 
 productName : "tv",
 locationName: " chennai"
}

Note: Tried SplitText, AttirtubesToJson processors not able to achieve desired conversion.

Upvotes: 1

Views: 11492

Answers (1)

Andy
Andy

Reputation: 14184

This is because the input data is not valid JSON. I recreated this flow locally and the error from EvaluateJsonPath is

2017-08-22 10:20:21,079 ERROR [Timer-Driven Process Thread-5] o.a.n.p.standard.EvaluateJsonPath EvaluateJsonPath[id=0aec27af-015e-1000-fac5-4e0f455a10fe] FlowFile StandardFlowFileRecord[uuid=b903eeb0-8985-4517-910f-5e3bbbccb8dc,claim=StandardContentClaim [resourceClaim=StandardResourceClaim[id=1503421928125-1, container=default, section=1], offset=376, length=47],offset=0,name=91708717370829,size=47] did not have valid JSON content.

Which condenses to [flowfile] did not have valid JSON content. The processor uses a strict validator and the input you're providing is not valid JSON. You'll need to use text manipulation or regexes to update this content to the following:

{"productName":"tv", "locationName":"chennai"}

Once you have accomplished this (via ReplaceText, etc.), the EvaluateJsonPath processor will work properly.

Also, to be clear, EvaluateJsonPath is designed to execute JSONPath expressions to extract JSON values to flowfile attributes. It is not designed to manipulate arbitrary text into JSON format.

Update

There is no universal process to convert arbitrary data to JSON. Given the specific input you provided, the following values for ReplaceText will convert this to valid JSON:

  • Search Value: (?<!\")(\w+)(?=[\s:])
  • Replacement Value: "$1"
  • Replacement Strategy: Regex Replace
  • Evaluation Mode: Entire text

If you get incoming data that is invalid in some other way, you'll have to modify this process. You may be interested in something like JSONLint to validate and format your incoming data.

Upvotes: 2

Related Questions