json from mongo to csv in scala
I am retrieving some records from mongo using scala. These are plain json records. I need to convert them to csv. Is there a method or a library that does this. As far as ive searched there are no such converters or libraries to do that in scala. Basically i wanna do something like this. The json will be something like but the fields are not known, but for a particular query the fields returned would be same. for example if i query for apple the entire result will have the same fields like
{ "id" : "some", "type" : "no-type", "extra" : "some开发者_Python百科data" }
Say there are 100 records, how do i find out the fields in these records and export them to a csv file.
Sadly for scala youre out of luck. You will need to decide on how you want your CSV file to be formatted and how you will deal with any missing tags (if possible).
There is a good example here, not in Scala but the process should be similar: http://michelleminkoff.com/2011/02/01/making-the-structured-usable-transform-json-into-a-csv/
For reading JSON and using Mongo checkout Beaucatcher: http://beaucatcher.org/
For writing files use the scala-io library.
Heres a good CSV parser in SCALA for future use: https://gist.github.com/115557
精彩评论