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.

NiFi CSV to Json with Json object string as value

Solved Go to solution
Highlighted

NiFi CSV to Json with Json object string as value

New Contributor

Hi, I am looking for the way to convert the CSV to JSON

I have a csv file with json object as value,

 

"name","item"

"david","{""currency"":""USD"",""content"":""Beauty Lips""}"

 

 

I used ConvertRecord processor with CSVReader and JsonRecordSetWriter

And the output JSON is

 

{
  "name":"david",
  "item":"{\"currency\":\"USD\",\"content\":\"Beauty Lips\"}"
}

 

But what I expected is

 

{
  "name":"david",
  "item":{"currency":"USD","name":"Beauty Lips"}
}

 

 

How can I archive this?

Thank you

 

David

1 ACCEPTED SOLUTION

Accepted Solutions

Re: NiFi CSV to Json with Json object string as value

New Contributor

I re0rder the json to

 

 

 

{
  "item":{"currency":"USD","name":"Beauty Lips"},"name":"david"
}

 

 

 

I used ReplaceText processor

search value: "item":(.*),"name"

 

replace value: "item":${'$1':replaceFirst('"', ''):substringBeforeLast('"')},"name"

 

It does not look beauty but it works =/

 

My question is why I don't need to use unexcapeJson() like this?

"item":${'$1':replaceFirst('"', ''):substringBeforeLast('"'):unescapeJson()},"name"

1 REPLY 1

Re: NiFi CSV to Json with Json object string as value

New Contributor

I re0rder the json to

 

 

 

{
  "item":{"currency":"USD","name":"Beauty Lips"},"name":"david"
}

 

 

 

I used ReplaceText processor

search value: "item":(.*),"name"

 

replace value: "item":${'$1':replaceFirst('"', ''):substringBeforeLast('"')},"name"

 

It does not look beauty but it works =/

 

My question is why I don't need to use unexcapeJson() like this?

"item":${'$1':replaceFirst('"', ''):substringBeforeLast('"'):unescapeJson()},"name"

Don't have an account?
Coming from Hortonworks? Activate your account here