CSV to JSON Converter for Swift
Free online csv to json converter with Swift code examples
Working with csv to json converter in Swift? Our free online csv to json converter helps Swift developers format, validate, and process data instantly. Below you will find Swift code examples using CodableCSV so you can achieve the same result programmatically in your own projects.
Try the CSV to JSON Converter Online
Use our free CSV to JSON directly in your browser — no setup required.
Open CSV to JSONSwift Code Example
import Foundation
let csv = "name,age,city\nAlice,30,New York\nBob,25,London"
let lines = csv.components(separatedBy: "\n")
let headers = lines[0].components(separatedBy: ",")
var result: [[String: String]] = []
for line in lines.dropFirst() {
let values = line.components(separatedBy: ",")
var dict: [String: String] = [:]
for (i, header) in headers.enumerated() {
dict[header] = values[i]
}
result.append(dict)
}
let jsonData = try JSONSerialization.data(withJSONObject: result, options: .prettyPrinted)
print(String(data: jsonData, encoding: .utf8)!)Quick Setup
Library: CodableCSV
// SPM: https://github.com/dehesa/CodableCSVSwift Tips & Best Practices
- CodableCSV provides Codable-based CSV parsing
- For simple CSV, string splitting works fine
- Handle quoted fields with proper CSV libraries for production