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

Announcements
Join us in Toronto Sept 9th for Qlik's AI Reality Tour! Register Now
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

If the CSV is only one line, JSON data with a different layout will be created.

Hi.

 

I have a CSV file that looks like this:

id,name,age
1,john,27
2,paul,30

I want to create JSON array like this:

[
    {"id":1,"name":"john","age":27},
    {"id":2,"name":"paul","age":30}
]

There is no problem if there are more than 2 rows in the CSV file.

 

But, if there is 1 line, it will be output like this.

Input:

id,name,age
1,john,27

Output:

{
    "dummyloop":{"id":1,"name":"john","age":27}
}

 

Why does the layout change?
I want the following results:

[{"id":1,"name":"john","age":27}]

 

I present a job with the minimum configuration that can reproduce this phenomenon.

(In fact, the job continues after this.)

0683p000009M835.pngJob Overview0683p000009M83A.pngtFileInputDelimited0683p000009M7oA.pngtMap0683p000009M83F.pngtFileInputJSONField - Component0683p000009M83K.pngtFileInputJSONField - Schema0683p000009M83P.pngtFileInputJSONField - JSON tree

 

Q:Why use a dummy loop?

A:The job continues after this.
This is the final layout:

{
  "app" : 9,
  "records" : [
    {"id":1,"name":"john","age":27},
    {"id":2,"name":"paul","age":30}
  ]
}

Thank you.

Labels (3)
1 Reply
Anonymous
Not applicable
Author

Hi yanchi
If there is only one row, you need to add an attribute @class under dummyloop element, and set its value as "array", see image.

0683p000009M8b3.png

It will generate a string like:

{"dummyloop":[{"id":"1","name":"john","age":"27"}]}

After that, you can remove other characters with this expression on a tJavaRow:

output_row.newColumn = input_row.newColumn.substring(input_row.newColumn.indexOf("["), input_row.newColumn.lastIndexOf("]")+1);

 

The result will be:

[{"id":"1","name":"john","age":"27"}]

 

Regards

Shong