Support Questions

Find answers, ask questions, and share your expertise

NiFi - JOLT to get json array as comma separated string

avatar
Master Collaborator

Hello,

I have below json as flowfile content -

{

"organization_id": "life-360",

"message_type_id": "MSG01",

"consent_required": "1",

"country_list": [ "GB", "IT", "BG" ]

}

I want below content as output -

GB, IT, BG

I am able to get

"country_list": [ "GB", "IT", "BG" ] in flow file using JOLT but not comma separated country list.

Could any one please help with best way?

Thanks,

Mahendra

1 ACCEPTED SOLUTION

avatar
Master Guru
@Mahendra Hegde

AFAIK we cannot extract only the value using Jolt transform.

To achieve this extract the array from the json using `EvaluateJsonPath` processor

EvaluateJsonPath Configs:

93371-screen-shot-2018-11-22-at-71800-pm.png

Now using ReplaceText processor replace ",[,],space from the extracted value using NiFi expression language.

ReplaceText Configs:

93372-screen-shot-2018-11-22-at-72003-pm.png

Search Value

(?s)(^.*$)

Replacement Value

${country_list:replaceAll('(\[|\]|"|\s)','')}

Character Set

UTF-8

Maximum Buffer Size

1 MB

Replacement Strategy

Always Replace

Evaluation Mode

Entire text

Input:

{
"organization_id": "life-360",
"message_type_id": "MSG01",
"consent_required": "1",
"country_list": [ "GB", "IT", "BG" ]
}

Output:

GB,IT,BG

Flow:

1.other processors
2.EvaluateJsonPath //extract the json array as attribute value
3.ReplaceText //replace the content of flowfile with extracted value
4..other processors

View solution in original post

2 REPLIES 2

avatar
Master Guru
@Mahendra Hegde

AFAIK we cannot extract only the value using Jolt transform.

To achieve this extract the array from the json using `EvaluateJsonPath` processor

EvaluateJsonPath Configs:

93371-screen-shot-2018-11-22-at-71800-pm.png

Now using ReplaceText processor replace ",[,],space from the extracted value using NiFi expression language.

ReplaceText Configs:

93372-screen-shot-2018-11-22-at-72003-pm.png

Search Value

(?s)(^.*$)

Replacement Value

${country_list:replaceAll('(\[|\]|"|\s)','')}

Character Set

UTF-8

Maximum Buffer Size

1 MB

Replacement Strategy

Always Replace

Evaluation Mode

Entire text

Input:

{
"organization_id": "life-360",
"message_type_id": "MSG01",
"consent_required": "1",
"country_list": [ "GB", "IT", "BG" ]
}

Output:

GB,IT,BG

Flow:

1.other processors
2.EvaluateJsonPath //extract the json array as attribute value
3.ReplaceText //replace the content of flowfile with extracted value
4..other processors

avatar
Master Collaborator

Thanks @Shu !

It worked, I was using 'Return type' as auto instead of json in EvaluateJsonPath.

Thanks for the reply 🙂