Created 03-11-2022 03:16 AM
Hi,
I am trying to convert json to csv in nifi. My json is something like below
{"tokens":["12343455577666","2134421241241","124142141411445","21425654768686"]}
and my csv should be like below
tokens
12343455577666
2134421241241
124142141411445
21425654768686
Please help me. Thanks!
Created 03-11-2022 03:58 AM
There are a few posts on this topic. The nearest to your need with a solution is
https://community.cloudera.com/t5/Support-Questions/NiFi-JSON-Array-split/m-p/158842#M121233
The key being using the SplitJson processor the updated doc reference is
https://nifi.apache.org/docs/nifi-docs/components/nifi-docs/components/org.apache.nifi/nifi-standard...
This will need to be used in combination with Reader > JsonPathReader > SplitJson > Writer
Created 03-14-2022 07:38 PM
Hi, @VikasNayak07 ,
An easy way to achieve this is to use a JoltTransformRecord processor with a JsonTreeReader and a CSVRecordSetWriter. The following "Chain" transformation will produce the output that you want:
[
{
"operation": "shift",
"spec": {
"tokens": {
"*": "[&].token"
}
}
}
]
Cheers,
André
--
Was your question answered? Please take some time to click on "Accept as Solution" below this post.
If you find a reply useful, say thanks by clicking on the thumbs up button.
Created 10-02-2023 09:19 AM
@araujo
I am trying to implement your suggested way.
My json is :
{
"Id" : "some int",
"TenantId" : "some double value"
}
and I am using chain jolt transform method as:
[
{
"operation": "shift",
"spec": {
"*": {
"@(1,Id)": "[&1].Id",
"@(1,TenantId)": "[&1].TenantId"
}
}
}]
However, I am getting a error as:
Can you tell where I am going wrong?
Thanks.
Created 10-02-2023 09:32 AM
from the error message I can see that your input seems to be in avro format. Are you sure the flowfile has valid json format? If not you can use ConvertAvroToJson processor before using the jolt transformation to convert avro to json. Also from the json you provided not sure your jolt spec will work. If you provide your json input and the expected output more accurately maybe I can help you with defining the correct jolt spec.
Created 10-02-2023 09:40 AM
@SAMSAL
thank you for responding.
I am infact using/converting the output from ExecuteSql from Arvo to Json using ConvertAvroToJson processor.
Below is the output of the same processor:
I am also wondering if I am providing the json format, its throwing error for avro.
Can you suggest something?
Created 10-02-2023 10:03 AM
I have to look into the JoltTranformationRecord to see what configuration you have there vs what is expected. However since you are having a json input can you use the JoltTransformationJson processor instead or are you trying to convert the output json into different format?
Created 10-02-2023 10:12 AM
Thats right. I intend to convert the json using JoltTransfomationRecord into csv format and then save the file in my local machine.
Created 10-02-2023 10:25 AM
Can you take screenshot of the JoltTranformRecord and the reader\write services settings?
Created 10-02-2023 02:46 PM
Here
JoltTransforRecord:
JsonTreeReader:
AvroReader:
Thanks.