We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi,
I've written the following code to convert a CSV file into JSON and it all works fast and efficiently. The only issue I have is that it produces an 'unminified' JSON format rather than my preferred format of 'minified'. Is there a simple change I could make to the program or an option I could use to produce a minified format?
Many thanks!
Table csv = loadTable("D:/09SEP16.csv", "header");
JSONArray values = new JSONArray();
int i=0;
String isoDate;
for (TableRow r : csv.rows ()) {
JSONObject json = new JSONObject();
try{
isoDate = new SimpleDateFormat("yyyy-MM-dd").format(new SimpleDateFormat("ddMMMyy").parse(r.getString("DATE")));
}
catch (Exception e){
println("Date Error");
isoDate = "01-01-2017";
}
json.setString("T", r.getString("TIME"));
json.setInt("S", r.getInt("S"));
json.setString("U", r.getString("USER"));
json.setString("F", r.getString("FULLPATH"));
json.setString("i", isoDate);
values.setJSONObject(i++, json);
}
saveJSONArray(values, "D:/09SEP16.json");
println(csv.getRowCount());
exit();
Answers
The reference tells us that
saveJSONArray()
takes anoptions
argument.The PApplet source tells us that
saveJSONArray()
callsJSONArray#save()
.The JSONArray source tells us that
save()
eventually callswrite()
.Reading the source for the
write()
function, we can see that one of the available options is"compact"
.Finally, we see that the
write()
function callswriteInternal()
, which tells us that it eliminates newlines and indentation if you pass-1
into it, which is the case when you pass in"compact"
as an option tosaveJSONArray()
.All of that to say, try this:
Knowing about the possibility of "compact" seems useful for JSON workflows that I suggested adding to the top-level documentation as an enhancement: issue 4683.