Hacker News new | ask | show | jobs
by EvanPlaice 3741 days ago
Yes. Use the built-in JSON.parse() to get the data into memory, then use my jquery-csv library's $.csv.fromArrays() to format the data as CSV.

Guess I found a good candidate for a blog post. It's amazing How much I see others struggle with such a aimple concept.

1 comments

Not sure if you parsed my comment incorrectly or you meant to reply to someone else :)
Somehow missed the 'java/scala part'.

Either way, why do 2 data format transforms as one lib? It makes more sense to parse JSON into memory in one step then format it as CSV in a separate step.

Ie, what about the 'Single Responsibility Principle'?

1) The parsing and converting are done with separate methods. Thus single responsibility.

2) It's a very small library. Less than 200 lines (including comments). There's no reason to put this in 2 jars, classes or packages. Have you looked at the code at all?

3) The method call to parse JSON is in the method that converts the CSV, but it's trivial to move it out and run it explicitly. Simply remove the call to parse and explicitly run it separate.

4) Client wanted the ability to filter out JSON data they didn't want in the CSV. Thus, it's a little more complicated than a call to something like JSON.parse() in Scala/Java.

5) Code does what it needed to do and made the client happy.

If you think you can do it better while keeping the code in Java/Scala, send a pull request to my repo and we'll discuss :)