When you convert CSV to and from JSON(/XML) then the interesting part IMHO is how to convert the hierarchy (of JSON/XML) to CSV and how to encode it (e.g. in a header-line). Do you account for that?
There are some assumptions made. Mainly, that you have an array of arrays or an array of objects to encode as CSV. For an array of arrays, that's easy: each array is a row. For an array of objects, each object is a row keyed by field name.
Data with a varied structure is much trickier, if at all possible, to flatten. I haven't figured out a magic formula for that (yet?).
Thanks for scrolling down :) and answering. Yes assumptions have to be made. I have just been curious, since I have done sth similar recently (http://www.use-the-tree.com). The assumptions I make there are "that these are reasonable business documents" (e.g. invoices, orders). E.g. A List of Customers having Orders with Order Lines (3 levels (or more))