Convert multiple JSON Strings to CSV - java

I need help converting a bunch of json strings to CSVs inside a loop
for (MyBusinessObj obj:MyBusinessObjects){
String json = convertBOToJson();
Need to add this to CSV (only one header row)
}
Each string looks like below:
String1 {"name":"Ram","Age":"80",".....}
String2 {"name":"Rick","Age":"67",".....}
I need a CSV like:
name age
Ram 80
Rick 67
Each string can be close to 50MB, and I could have millions of rows. so I cannot construct one giant JSON string so I can get a JSONArray. How do I add these strings to a CSV file?
I have to use base Java, or some common library like openCSV

Related

is there a way to add double quotes to fields splitted in two lines during parsing csv file through opencsv?

I am getting a big production data in a csv file where some records are broken into two or more lines due to new line characters in the record. But the problem is that the field values that are broken into multiple lines are not enclosed in the double quotes.
I am parsing the file using OpenCSV API (using readNext method of **
com.opencsv.CSVReader**) in java. Do we have anything in OpenCSV or in any other API that identify the record split and enclose the splitted values in the record with double quotes, so that readNext method consider it as one record in-spite of new line character involved in the record.
Actually the point is, I don't want to write any complex logic to assemble the splitted records, as there can be column values for the record that can be less than or equal to the header count in the file.

Java - CSVReader split correctly with comma inside the values

I am reading a csv file in java using CSVReader. It is "working" but I've found a minor problem when I try to split the line into an array of strings like this:
reader = new CSVReader(new FileReader(csvFile));
line = reader.readNext()
String[] lineDetail = line[0].split(";", -1);
Here is my problem: The line below work correctly:
[ABEL MESQUITA JR.;178957;1;2015;RR;DEM;55;1;MANUTENÇÃO DE ESCRITÓRIO DE APOIO À ATIVIDADE PARLAMENTAR;0;;WM PAPELARIA E ESCRITÓRIO;12132854000100;3592;0;2017-04-26 00:00:00;296;0;296;4;2017;0;;;1377952;5828;0;3074;6266962]
But the line below when I try to read using CSVReader, results in 3 arrays of strings:
[ABEL MESQUITA JR.;178957;1;2015;RR;DEM;55;3;COMBUSTÍVEIS E LUBRIFICANTES.;1;Veículos Automotores;B.B. PETROLEO LTDA;03625917000170;4339;0;2017-01-31 00:00:00;4007, 06;0;4007, 06;1;2017;0;;;1354058;5711;0;3074;6196889]
The arrays look like this:
ABEL MESQUITA JR.;178957;1;2015;RR;DEM;55;3;COMBUSTÍVEIS E LUBRIFICANTES.;1;Veículos Automotores;B.B. PETROLEO LTDA;03625917000170;4339;0;2017-01-31 00:00:00;4007
06;0;4007
06;1;2017;0;;;1354058;5711;0;3074;6196889
I think the problem is because of this value: 4007, 06 since in the first line the value is an integer 296.
Does anyone know how to make the CSVReader returns only one array, instead of 3?
Thanks in advance!!
EDIT 1
The result that I need is the second and third array concatenated with the first. So I would have the 4007,06 together instead of separated.
Your line of CSV data is getting split on comma, which is default CSV field separator.
To avoid splitting on commas, before reading your CSV file initialize your CSV reader to use some unused character (for example Tab) as CSV field separator. This way you will get one field per row. But your expectation seems to be pointless, because normally there should be easier way:
Why you don't configure CSV field separator to ; so it will split your CSV directly into semicolon-separated fields and you won't need to do additional splitting? This will also solve your problem with commas.
Your line[0].split(";", -1) is basically a bug, because it won't be able to split this valid CSV into 2 values:
Value 1; "Value;2"
Using your approach you will get 3 values instead.
To get further advice on CSVReader, please add the information which one are you using (by denoting its package).

How to save some content into a column and read it back to an object

I have string array. I need this string array to be saved as a comma separated single string inside a single text column. But when i read and write the hibernate entity i will just set and get the List<String>. I want to know a sample and how can I do it with hibernate annotations.
You can use Hibernate UserTypes.
https://docs.jboss.org/hibernate/orm/3.6/javadocs/org/hibernate/usertype/UserType.html

Converting Text Received via JMS into XML

I am trying to convert a message which i receive through JMS, which is in this form "Harry PotterJ.K.Rowling1995"
This is stored as a String. For example String contents.
How do i convert this String contents into an XML file.
Thanks you so much for your help in advance.
Smart ass answer:
String xmlString = "<myNewXmlString>" + myString + "</mynewXmlString>";
If it's more complicated you may want to look at this:
http://xerces.apache.org/xerces-j/
You probably also need some delimiter (a comma or colon separating the values) of the string to know What is a title, author and year. You'd then need separate tags for each. Another way to solve that is to use fixed width columns. So authors are 50, titles are 50, and years are 4. There are really three steps here: tokenizing the string, parsing the string into a data structure, and outputting the data structure to XML.
There are lots of automated tools that can convert a data structure like this:
class Book {
String title;
String author;
Date year;
}
into XML as well.
See: http://jackson-users.ning.com/profiles/blogs/experimental-support-for

How to properly parse CSV file to 2d Array?

I'm trying to parse a csv file into a 2d array, where each row is a data entry and each column is a field in that entry.
Doing this all at once simplifies and separates my processing code from my parsing code.
I tried to write a simple parser that used String.Split to separate file by commas. This is a horrible approach as I have discovered. It completely fails to parse any special cases like double quotes, line feeds, and other special chars.
What is the proper way to parse a CSV file into a 2d array as I have described?
Code samples in Java would be appreciated.
The array can be a dynamic list object or vector or something like that, it just has to be indexable with two indexers.
Have a look at Commons CSV?
CSVParser parser = new CSVParser(new FileReader(file));
String[] line;
while ((line = parser.getLine()) != null) {
// process
}
If your file has fields with double quoted entries that contain separators and fields with line feeds, than I doubt that it is a real csv file... a proper csv file is something like this
1;John;Doe;engineer,manager
2;Bart;Foo;engineer,dilbert
while this is "something else":
1;John;Doe;"engineer;manager"
2;Bart;Foo;
"engineer,dilbert"
And the first example is parseable with String.split on each line.

Categories