3 ms·
Thanks, I've used your tool a few times and it works beautifully. Quite interesting article too from the SEO pov. BTW, does anyone know of a tool to do the rev
by kawera 11y ago
Thanks, I've used your tool a few times and it works beautifully. Quite interesting article too from the SEO pov.
BTW, does anyone know of a tool to do the reverse? I have some json files with deeply nested arrays and would like to output a csv file with one line for each "leaf". I can write it myself but would be happy to use an existing tool.
- martin_drapeau 11y agoI suggest you go to Github and open an issue: https://github.com/martindrapeau/csvjson-app/issues https://github.com/martindrapeau/csvjson-app/issues From there, I'll be happy to code it.
- matthewmacleod 11y agojq (https://stedolan.github.io/jq/ https://stedolan.github.io/jq/) is a great tool for general JSON manipulation. It includes a CSV export filter, so you can run commands like: jq -r '. | map(.key), map(.value) | @csv' which will output a JSON array as CSV.
- kawera 11y agoVery elegant solution, thank you. jq is indeed a powerful tool but it can get hairy with obnoxiously intricate json files/streams.
- jsnell 11y agoOne that I wrote a while ago, which might or might not do what you want: https://github.com/jsnell/json-to-multicsv https://github.com/jsnell/json-to-multicsv Note that the mapping from nested / hierarchical JSON file to CSV file(s) is not as obvious as it is the other way around. There's some configuration involved. I wrote a blog post about that particular aspect: https://www.snellman.net/blog/archive/2016-01-12-json-to-multicsv/ https://www.snellman.net/blog/archive/2016-01-12-json-to-mul...
- kawera 11y agoI tried but json-to-multicsv didn't really solve this particular case as I have some weird json files. Not been well versed in perl, I ended up writing a little script in python and, for that, your blog post was very helpful, thank you!
- yareally 11y ago> BTW, does anyone know of a tool to do the reverse? I created a java/scala library[1] (source is scala, but there's a jar with dependencies for java) a few years ago for an Android app I was hired to work on that converts nested JSON to CSV. I was allowed to open source the library later on and released it under the MIT License, so anyone can use it now. Never thought anyone else would ever need to convert json to csv, but I guess I was wrong :) I don't have a use case for it anymore, so I haven't maintained it, but if someone wants to update it, feel free to send a pull request. [1] https://github.com/yareally/json-to-csv https://github.com/yareally/json-to-csv
- EvanPlaice 11y agoYes. 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.
- yareally 11y agoNot sure if you parsed my comment incorrectly or you meant to reply to someone else :)
- EvanPlaice 11y agoSomehow 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'?
- yareally 11y ago1) 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 :)