Support Questions
Find answers, ask questions, and share your expertise
Announcements
Alert: Welcome to the Unified Cloudera Community. Former HCC members be sure to read and learn how to activate your account here.

how to extract fields in flow file which are surrounded by square brackets

Solved Go to solution

how to extract fields in flow file which are surrounded by square brackets

New Contributor

Hi

How extract fields from flow file in json format are in square brackets. I am using Evaluate json path processor but not getting results . Please help me on this regards

[{"projetname":"ABC", "projectid": "ABC-12", "startdate" :"Aug-2018"}]

I was used the evaluate json path processor : projectidvariable --> "projectid":\s(.*?),

Thanks for your time and consideration

1 ACCEPTED SOLUTION

Accepted Solutions

Re: how to extract fields in flow file which are surrounded by square brackets

Super Guru

@Suresh Dendukuri

EvaluateJsonPath processor accepts only valid json path's(ex:$.<key>..) as you are specifying Regex if you want to extract the values using Regex then use ExtractText processor and add your regex path in that processor.

Configure EvaluateJsonPath processor as shown below and keep Destination as flowfile-attributes and add new properties

91650-screen-shot-2018-10-08-at-60056-pm.png

projectid

$.[0].projectid

projetname

$.[0].projetname

startdate

$.[0].startdate

Output:

91651-screen-shot-2018-10-08-at-60256-pm.png

As you see in the above screenshot attributes are added to the flowfile, This way we are assuming there are only one json message in array and extracting the first message values and storing as flowfile attributes.

Cleaner way to do this task as follows:

as you are having array of json messages and want's to extract the values of the json key's so split the array of json messages into individual messages by using SplitJson processor.

SplitJsonConfigs:

91652-screen-shot-2018-10-08-at-60522-pm.png

Then use EvaluateJsonPath processor:

91653-screen-shot-2018-10-08-at-60631-pm.png

Configure the processor as show above this will work if you are having more than one json message in an array also.

3 REPLIES 3

Re: how to extract fields in flow file which are surrounded by square brackets

Super Guru

@Suresh Dendukuri

EvaluateJsonPath processor accepts only valid json path's(ex:$.<key>..) as you are specifying Regex if you want to extract the values using Regex then use ExtractText processor and add your regex path in that processor.

Configure EvaluateJsonPath processor as shown below and keep Destination as flowfile-attributes and add new properties

91650-screen-shot-2018-10-08-at-60056-pm.png

projectid

$.[0].projectid

projetname

$.[0].projetname

startdate

$.[0].startdate

Output:

91651-screen-shot-2018-10-08-at-60256-pm.png

As you see in the above screenshot attributes are added to the flowfile, This way we are assuming there are only one json message in array and extracting the first message values and storing as flowfile attributes.

Cleaner way to do this task as follows:

as you are having array of json messages and want's to extract the values of the json key's so split the array of json messages into individual messages by using SplitJson processor.

SplitJsonConfigs:

91652-screen-shot-2018-10-08-at-60522-pm.png

Then use EvaluateJsonPath processor:

91653-screen-shot-2018-10-08-at-60631-pm.png

Configure the processor as show above this will work if you are having more than one json message in an array also.

Re: how to extract fields in flow file which are surrounded by square brackets

New Contributor

Hey can you please help me with extracting regex attribute from a json file. I am trying to take attribute from a json file, this attributes have regex values. For ex: json file is - { "key1" : "(.{2})(.{4})", "key2" : "$1,$2"}

From this json file I would need to take key1 and key2 in replacetext processor to search(key1) and replace by (key2). Please suggest a way to do this. Thanks!!

Re: how to extract fields in flow file which are surrounded by square brackets

New Contributor

Hi

@Shu thank you very much for your help and support . I willtry this solution