Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Qlik Open Lakehouse is Now Generally Available! Discover the key highlights and partner resources here.
cancel
Showing results for 
Search instead for 
Did you mean: 
YPMAL
Contributor III
Contributor III

need to convert delimited file to key value pair

I have input file ..

eg.

-------------------------------------------------------------      

name,number,country                                                        

yogesh,420420,India                                                         This is my csv file with headers and data

-------------------------------------------------------------

 

and  i want output file like

--------------------------------------------------------------

"name":yogesh,"number":420420,"country":India 

--------------------------------------------------------------

Labels (3)
1 Solution

Accepted Solutions
vapukov
Master II
Master II

I personally do this by next way (sure it possible todo different):

First of all, You need:

apache library - commons-lang-2.6.jar

than You can download from Talend Exchange (when talend Make it live !!! 🙂 ) routines - StringUtils and setup decencies in Studio:

 

0683p000009Lu43.png0683p000009LuTI.png

 

 

I use standard tFileInputDelimited component:

 

0683p000009LuQ4.png

 

 

 

for example in this Job, file have comma separator, but I use TAB as separator in component:

1st iteration for header:

 

 

0683p000009LuP4.png

 

 

which use limit 1 line - read only header line, component have only 1 column in schema - line, type String

than I parse header line to array:

 

 

0683p000009LukF.png0683p000009LuUr.png

 

 

Than read same file again, but with skipped line 1

 

 

0683p000009LuMV.png

 

 

split each row to array

 

 

0683p000009LukK.png

 

 

 

and finally - concat JSON from 2 arrays:

 

 

0683p000009LukP.png

 

 

as result You will have output JSON column, You can remove quotes { } from code if You do not need them

 

View solution in original post

13 Replies
Anonymous
Not applicable

OK, set your tInputFileDelimited component up to have 3 columns (for each of your file columns) and to ignore the first row (as a header). The separator should be a comma. Connect this to a tMap. On the output of the tMap, connect all of your columns (name to name, number to number and country to country). In each of the output columns, hard code the relevant String to describe the value. As an example.....

 

"\"name\":"+ row1.name

 

The Strings are encapsulated in quotes because they are literal values. The \" is used to allow quotes to be included in the value. The above would result in a value like below if the name for that row was Richard....


"name":Richard

 

 

Hope that helps

vapukov
Master II
Master II

You can use array functions (import java.util.Arrays)

 

read file row by row:

1) step 1 - split header line to array and save array size

2) step 2 - for each row split row to array

3) in loop (x from 0 to (array size -1) ) concat

"\""+header[x]+"\":"+value[x]

or concat

 

"{" than in loop 
"\""+header[x]+"\":"+"\""+value[x]+"\""
than "}"

for have valid JSON

 

I use this for create json structure from unknown csv (any number of columns)

 

YPMAL
Contributor III
Contributor III
Author

Hi @rhall, Thanks for quick reply but I am looking for solution when number of columns are not known.

vapukov
Master II
Master II


@yogeshmalekar wrote:

Hi, Thanks for quick reply but I am looking for solution when number of columns are not known.


🙂

 

@vapukov wrote:

 

I use this for create json structure from unknown csv (any number of columns)

 

YPMAL
Contributor III
Contributor III
Author

Hi Vapukov, I will try and will ask if come across any problem. I need to find how to read file row by row in routine or in java code.

vapukov
Master II
Master II

I do this in tJavaFlex

 

tFileInputDelimited - but delimiter different than real. Like | with real delimiter comma (,)

than tJavaFlex

 

I repeat it twice - 1st iteration for only 1st row, 2nd for all other with skipped 1st

YPMAL
Contributor III
Contributor III
Author

Hi vapukov,

    Please tell me how to read entire row at a time to store in a array and count the number of values in it using delimiter.

vapukov
Master II
Master II

I personally do this by next way (sure it possible todo different):

First of all, You need:

apache library - commons-lang-2.6.jar

than You can download from Talend Exchange (when talend Make it live !!! 🙂 ) routines - StringUtils and setup decencies in Studio:

 

0683p000009Lu43.png0683p000009LuTI.png

 

 

I use standard tFileInputDelimited component:

 

0683p000009LuQ4.png

 

 

 

for example in this Job, file have comma separator, but I use TAB as separator in component:

1st iteration for header:

 

 

0683p000009LuP4.png

 

 

which use limit 1 line - read only header line, component have only 1 column in schema - line, type String

than I parse header line to array:

 

 

0683p000009LukF.png0683p000009LuUr.png

 

 

Than read same file again, but with skipped line 1

 

 

0683p000009LuMV.png

 

 

split each row to array

 

 

0683p000009LukK.png

 

 

 

and finally - concat JSON from 2 arrays:

 

 

0683p000009LukP.png

 

 

as result You will have output JSON column, You can remove quotes { } from code if You do not need them

 

YPMAL
Contributor III
Contributor III
Author

0683p000009LuYG.png

 

 

0683p000009Lug5.png

 

 

0683p000009Luke.png

 

 

0683p000009Lukj.png

 

 

0683p000009LuZ8.png

 

 getting this error... couldn't understand the problem....

0683p000009Luko.png