How to parse a CSV with JSON fields containing separator?












0















I have the following CSV file with a JSON field that may contains the separator:



2.0.0|2018-11-04 10:10:14,471|eventname|{"fieldA":"value with separator | inside","fieldB":"54321"}|123456|empty


I tried to use the following code to get the whole field but it is always get split by the inner separator:



String csvLine = "2.0.0|2018-11-04 10:10:14,471|eventname|{"fieldA":"value with separator | inside","fieldB":"54321"}|123456|empty"; 

CsvParserSettings settings = new CsvParserSettings();
settings.getFormat().setDelimiter("|");
settings.getFormat().setQuote('"');
settings.getFormat().setQuoteEscape('\');

CsvParser parser = new CsvParser(settings);

String row = parser.parseLine(csvLine);
System.out.println(row[3]);


Expected output:



{"fieldA":"value with separator | inside","fieldB":"54321"}


Current output:



{"fieldA":"value with separator


How can I tell the CSV parser to not split when the file is between quotes?










share|improve this question

























  • That CSV file is corrupt, since it didn't quote / escape the value containing a separator character. Don't try to parse it. Fix the code creating the file instead.

    – Andreas
    Nov 16 '18 at 1:35













  • Unfortunately, it's not possible to change the file write. I have to read them in this exact format.

    – Mike Dias
    Nov 16 '18 at 4:42











  • Then fix the line, by changing | to | if the number of preceding " is odd.

    – Andreas
    Nov 16 '18 at 16:46













  • FYI: If the line contains ", then you need to write String csvLine = "2.0.0|2018-11-04 10:10:14,471|eventname|{\"fieldA\":\"value with separator | inside\",\"fieldB\":\"54321\"}|123456|";

    – Andreas
    Nov 16 '18 at 16:48
















0















I have the following CSV file with a JSON field that may contains the separator:



2.0.0|2018-11-04 10:10:14,471|eventname|{"fieldA":"value with separator | inside","fieldB":"54321"}|123456|empty


I tried to use the following code to get the whole field but it is always get split by the inner separator:



String csvLine = "2.0.0|2018-11-04 10:10:14,471|eventname|{"fieldA":"value with separator | inside","fieldB":"54321"}|123456|empty"; 

CsvParserSettings settings = new CsvParserSettings();
settings.getFormat().setDelimiter("|");
settings.getFormat().setQuote('"');
settings.getFormat().setQuoteEscape('\');

CsvParser parser = new CsvParser(settings);

String row = parser.parseLine(csvLine);
System.out.println(row[3]);


Expected output:



{"fieldA":"value with separator | inside","fieldB":"54321"}


Current output:



{"fieldA":"value with separator


How can I tell the CSV parser to not split when the file is between quotes?










share|improve this question

























  • That CSV file is corrupt, since it didn't quote / escape the value containing a separator character. Don't try to parse it. Fix the code creating the file instead.

    – Andreas
    Nov 16 '18 at 1:35













  • Unfortunately, it's not possible to change the file write. I have to read them in this exact format.

    – Mike Dias
    Nov 16 '18 at 4:42











  • Then fix the line, by changing | to | if the number of preceding " is odd.

    – Andreas
    Nov 16 '18 at 16:46













  • FYI: If the line contains ", then you need to write String csvLine = "2.0.0|2018-11-04 10:10:14,471|eventname|{\"fieldA\":\"value with separator | inside\",\"fieldB\":\"54321\"}|123456|";

    – Andreas
    Nov 16 '18 at 16:48














0












0








0








I have the following CSV file with a JSON field that may contains the separator:



2.0.0|2018-11-04 10:10:14,471|eventname|{"fieldA":"value with separator | inside","fieldB":"54321"}|123456|empty


I tried to use the following code to get the whole field but it is always get split by the inner separator:



String csvLine = "2.0.0|2018-11-04 10:10:14,471|eventname|{"fieldA":"value with separator | inside","fieldB":"54321"}|123456|empty"; 

CsvParserSettings settings = new CsvParserSettings();
settings.getFormat().setDelimiter("|");
settings.getFormat().setQuote('"');
settings.getFormat().setQuoteEscape('\');

CsvParser parser = new CsvParser(settings);

String row = parser.parseLine(csvLine);
System.out.println(row[3]);


Expected output:



{"fieldA":"value with separator | inside","fieldB":"54321"}


Current output:



{"fieldA":"value with separator


How can I tell the CSV parser to not split when the file is between quotes?










share|improve this question
















I have the following CSV file with a JSON field that may contains the separator:



2.0.0|2018-11-04 10:10:14,471|eventname|{"fieldA":"value with separator | inside","fieldB":"54321"}|123456|empty


I tried to use the following code to get the whole field but it is always get split by the inner separator:



String csvLine = "2.0.0|2018-11-04 10:10:14,471|eventname|{"fieldA":"value with separator | inside","fieldB":"54321"}|123456|empty"; 

CsvParserSettings settings = new CsvParserSettings();
settings.getFormat().setDelimiter("|");
settings.getFormat().setQuote('"');
settings.getFormat().setQuoteEscape('\');

CsvParser parser = new CsvParser(settings);

String row = parser.parseLine(csvLine);
System.out.println(row[3]);


Expected output:



{"fieldA":"value with separator | inside","fieldB":"54321"}


Current output:



{"fieldA":"value with separator


How can I tell the CSV parser to not split when the file is between quotes?







java json csv univocity






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 11:07







Mike Dias

















asked Nov 16 '18 at 0:34









Mike DiasMike Dias

1239




1239













  • That CSV file is corrupt, since it didn't quote / escape the value containing a separator character. Don't try to parse it. Fix the code creating the file instead.

    – Andreas
    Nov 16 '18 at 1:35













  • Unfortunately, it's not possible to change the file write. I have to read them in this exact format.

    – Mike Dias
    Nov 16 '18 at 4:42











  • Then fix the line, by changing | to | if the number of preceding " is odd.

    – Andreas
    Nov 16 '18 at 16:46













  • FYI: If the line contains ", then you need to write String csvLine = "2.0.0|2018-11-04 10:10:14,471|eventname|{\"fieldA\":\"value with separator | inside\",\"fieldB\":\"54321\"}|123456|";

    – Andreas
    Nov 16 '18 at 16:48



















  • That CSV file is corrupt, since it didn't quote / escape the value containing a separator character. Don't try to parse it. Fix the code creating the file instead.

    – Andreas
    Nov 16 '18 at 1:35













  • Unfortunately, it's not possible to change the file write. I have to read them in this exact format.

    – Mike Dias
    Nov 16 '18 at 4:42











  • Then fix the line, by changing | to | if the number of preceding " is odd.

    – Andreas
    Nov 16 '18 at 16:46













  • FYI: If the line contains ", then you need to write String csvLine = "2.0.0|2018-11-04 10:10:14,471|eventname|{\"fieldA\":\"value with separator | inside\",\"fieldB\":\"54321\"}|123456|";

    – Andreas
    Nov 16 '18 at 16:48

















That CSV file is corrupt, since it didn't quote / escape the value containing a separator character. Don't try to parse it. Fix the code creating the file instead.

– Andreas
Nov 16 '18 at 1:35







That CSV file is corrupt, since it didn't quote / escape the value containing a separator character. Don't try to parse it. Fix the code creating the file instead.

– Andreas
Nov 16 '18 at 1:35















Unfortunately, it's not possible to change the file write. I have to read them in this exact format.

– Mike Dias
Nov 16 '18 at 4:42





Unfortunately, it's not possible to change the file write. I have to read them in this exact format.

– Mike Dias
Nov 16 '18 at 4:42













Then fix the line, by changing | to | if the number of preceding " is odd.

– Andreas
Nov 16 '18 at 16:46







Then fix the line, by changing | to | if the number of preceding " is odd.

– Andreas
Nov 16 '18 at 16:46















FYI: If the line contains ", then you need to write String csvLine = "2.0.0|2018-11-04 10:10:14,471|eventname|{\"fieldA\":\"value with separator | inside\",\"fieldB\":\"54321\"}|123456|";

– Andreas
Nov 16 '18 at 16:48





FYI: If the line contains ", then you need to write String csvLine = "2.0.0|2018-11-04 10:10:14,471|eventname|{\"fieldA\":\"value with separator | inside\",\"fieldB\":\"54321\"}|123456|";

– Andreas
Nov 16 '18 at 16:48












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329787%2fhow-to-parse-a-csv-with-json-fields-containing-separator%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329787%2fhow-to-parse-a-csv-with-json-fields-containing-separator%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine