Minified JSON

edited September 2016 in Questions about Code


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();
       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");


  • The reference tells us that saveJSONArray() takes an options argument.

    The PApplet source tells us that saveJSONArray() calls JSONArray#save().

    The JSONArray source tells us that save() eventually calls write().

    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 calls writeInternal(), 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 to saveJSONArray().

    All of that to say, try this:

    saveJSONArray(values, "D:/09SEP16.json", "compact");
  • 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.

Sign In or Register to comment.