Read & Parse local JSON file in Swift

ReadJsonFile

Read & Parse local JSON file in Swift

Usage

  1. Build model for your json data.

{
  "firstname": "Emrah",
  "lastname": "Turan",
  "website": "www.emrahturan.com.tr"
}

struct DataModel: Codable {
    var firstname: String
    var lastname: String
    var website: String
}
  1. Create a function to call your json file. Give json filename and model as parameter.

private func getData(completion: @escaping(DataModel?) -> Void) {
        readFromFile(fileName: "data", completion: completion)
}
  1. Data read from json file is ready to be used.

getData { data in
        guard let data = data else { return }

        print("firstname: ", data.firstname)
        print("lastname: ", data.lastname)
        print("website: ", data.website)
}

Feedback

If you have feedback about my ReadJsonFile Project do not forget to reach me via email or Discussions section of this repo.

GitHub

View Github