Convert CSV to JSON: Flatten, Nest, and Handle Edge Cases