Reading in a JSON File Using Swift
Asked Answered
I

35

308

I'm really struggling with trying to read a JSON file into Swift so I can play around with it. I've spent the best part of 2 days re-searching and trying different methods but no luck as of yet so I have signed up to StackOverFlow to see if anyone can point me in the right direction.....

My JSON file is called test.json and contains the following:

{
  "person":[
     {
       "name": "Bob",
       "age": "16",
       "employed": "No"
     },
     {
       "name": "Vinny",
       "age": "56",
       "employed": "Yes"
     }
  ]
}    

The file is stored in the documents directly and I access it using the following code:

let file = "test.json"
let dirs : String[] = NSSearchPathForDirectoriesInDomains(
                                                          NSSearchpathDirectory.DocumentDirectory,
                                                          NSSearchPathDomainMask.AllDomainMask,
                                                          true) as String[]

if (dirs != nil) {
    let directories: String[] = dirs
    let dir = directories[0]
    let path = dir.stringByAppendingPathComponent(file)
}

var jsonData = NSData(contentsOfFile:path, options: nil, error: nil)
println("jsonData \(jsonData)" // This prints what looks to be JSON encoded data.

var jsonDict = NSJSONSerialization.JSONObjectWithData(jsonData, options: nil, error: nil) as? NSDictionary

println("jsonDict \(jsonDict)") - This prints nil..... 

If anyone can just give me a push in the right direction on how I can de-serialize the JSON file and put it in an accessible Swift object I will be eternally grateful!

Kind Regards,

Krivvenz.

Incommensurable answered 25/6, 2014 at 14:4 Comment(4)
use the error parameter...Garbage
Please post actual, compilable code. As it is now, path is visible only in if scope, and unresolved when you use it in NSData(contentsOfFile, options, error); you also have typos in enum names.Hazlett
My API is fully updated for Swift 3: github.com/borchero/WebParsingUnderexpose
this is key -> "values": "%LOAD VALUE FROM tmclass.json file%" and i need to parse another JSON from file then how i can achieve this in SWIFT ?Baum
P
217

Swift 4.x and 5.x using Decodable

struct ResponseData: Decodable {
    var person: [Person]
}
struct Person : Decodable {
    var name: String
    var age: String
    var employed: String
}

func loadJson(filename fileName: String) -> [Person]? {
    if let url = Bundle.main.url(forResource: fileName, withExtension: "json") {
        do {
            let data = try Data(contentsOf: url)
            let decoder = JSONDecoder()
            let jsonData = try decoder.decode(ResponseData.self, from: data)
            return jsonData.person
        } catch {
            print("error:\(error)")
        }
    }
    return nil
}

Swift 3

func loadJson(filename fileName: String) -> [String: AnyObject]? {
    if let url = Bundle.main.url(forResource: fileName, withExtension: "json") {
        do {
            let data = try Data(contentsOf: url)
            let object = try JSONSerialization.jsonObject(with: data, options: .allowFragments)
            if let dictionary = object as? [String: AnyObject] {
                return dictionary
            }
        } catch {
            print("Error!! Unable to parse  \(fileName).json")
        }
    }
    return nil
}
Proven answered 24/4, 2016 at 19:38 Comment(1)
This should be moved to the new documentation feature, or marked as the correct answer.Excipient
T
380

Follow the below code :

if let path = NSBundle.mainBundle().pathForResource("test", ofType: "json")
{
    if let jsonData = NSData(contentsOfFile: path, options: .DataReadingMappedIfSafe, error: nil)
    {
        if let jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.MutableContainers, error: nil) as? NSDictionary
        {
            if let persons : NSArray = jsonResult["person"] as? NSArray
            {
                // Do stuff
            }
        }
     }
}

The array "persons" will contain all data for key person. Iterate throughs to fetch it.

Swift 4.0:

if let path = Bundle.main.path(forResource: "test", ofType: "json") {
    do {
          let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe)
          let jsonResult = try JSONSerialization.jsonObject(with: data, options: .mutableLeaves)
          if let jsonResult = jsonResult as? Dictionary<String, AnyObject>, let person = jsonResult["person"] as? [Any] {
                    // do stuff
          }
      } catch {
           // handle error
      }
}
Tousle answered 25/6, 2014 at 14:10 Comment(12)
It would be more helpful if you explained why/how this solves the problem described in the question, instead of just presenting a bunch of code.Petepetechia
Hi Abhishek - Thanks for your answer but it still doesn't work. That causes the application to crash with the below error: 2014-06-25 16:02:04.146 H & S Capture[4937:131932] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[_NSPlaceholderData initWithContentsOfFile:options:error:]: nil file argument' *** First throw call stack: Any ideas on why this is? For the jsonData options: I put (path, options: NSDataReadingOptions.DataReadingMappedIfSafe, error: nil)Incommensurable
Your file path is not correct. Acually, there is no file named test.json at the path specified by you. Please check for the correct location of the fileTousle
Its because I have the file saved in the documents directory rather than the application bundle.... The JSON file won't actually be there on install... it will be pulled from the net but I just wanted to make sure I could read the file once it was saved in the documents location....Incommensurable
Managed to resolve the issue by starting a new project and by re-doing the JSON file. I believe the JSON file had padding in it and wasn't pure JSON. I think it was previously saved as an RTF format. As such the above code mentioned by Abhishek works. So thanks alot!Incommensurable
"let jsonData = NSData(contentsOfFile:path!)" instead of "let jsonData = NSData.dataWithContentsOfFile(path, options: .DataReadingMappedIfSafe, error: nil)"Revocation
How do you write back to the file?Gadoid
It's better to use guard else statements here instead of this pyramid of doom though.Wernerwernerite
Since MacOS 10.6 / iOS 4 (!) there is an API url(forResource in (NS)Bundle to avoid the extra step to create the URL.Martines
OMG, Using URL(string:) instead of URL(fileURLWithPath:) was my problem! Thank you so much.Overstep
Bundle.main.path(forResource: "test", ofType: "json") is returning as nil in Swift 5.Bourgeois
Don't forget to add the file to your bundle. #41776063Malnourished
P
217

Swift 4.x and 5.x using Decodable

struct ResponseData: Decodable {
    var person: [Person]
}
struct Person : Decodable {
    var name: String
    var age: String
    var employed: String
}

func loadJson(filename fileName: String) -> [Person]? {
    if let url = Bundle.main.url(forResource: fileName, withExtension: "json") {
        do {
            let data = try Data(contentsOf: url)
            let decoder = JSONDecoder()
            let jsonData = try decoder.decode(ResponseData.self, from: data)
            return jsonData.person
        } catch {
            print("error:\(error)")
        }
    }
    return nil
}

Swift 3

func loadJson(filename fileName: String) -> [String: AnyObject]? {
    if let url = Bundle.main.url(forResource: fileName, withExtension: "json") {
        do {
            let data = try Data(contentsOf: url)
            let object = try JSONSerialization.jsonObject(with: data, options: .allowFragments)
            if let dictionary = object as? [String: AnyObject] {
                return dictionary
            }
        } catch {
            print("Error!! Unable to parse  \(fileName).json")
        }
    }
    return nil
}
Proven answered 24/4, 2016 at 19:38 Comment(1)
This should be moved to the new documentation feature, or marked as the correct answer.Excipient
P
158

If anyone is looking for SwiftyJSON Answer:
Update:
For Swift 3/4:

if let path = Bundle.main.path(forResource: "assets/test", ofType: "json") {
    do {
        let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .alwaysMapped)
        let jsonObj = try JSON(data: data)
        print("jsonData:\(jsonObj)")
    } catch let error {
        print("parse error: \(error.localizedDescription)")
    }
} else {
    print("Invalid filename/path.")
}
Prohibit answered 21/2, 2015 at 10:2 Comment(5)
this turned me onto swiftyJSON and carthage! thanks :)Futilitarian
i used it only to discover it lacks object mapping , i will try a different library next timeKashgar
To avoid copy paste error in swift 3: NSData and NSError became Data and Error.Recession
I tried a few different methods and this worked the best for me in Swift 3Gazpacho
Since MacOS 10.6 / iOS 4 (!) there is an API url(forResource in (NS)Bundle to avoid the extra step to create the URLMartines
H
45

Swift 5.1, Xcode 11

You can use this:


struct Person : Codable {
    let name: String
    let lastName: String
    let age: Int
}

func loadJson(fileName: String) -> Person? {
   let decoder = JSONDecoder()
   guard
        let url = Bundle.main.url(forResource: fileName, withExtension: "json"),
        let data = try? Data(contentsOf: url),
        let person = try? decoder.decode(Person.self, from: data)
   else {
        return nil
   }

   return person
}
Homebred answered 12/5, 2020 at 3:15 Comment(0)
I
24

Xcode 8 Swift 3 read json from file update:

    if let path = Bundle.main.path(forResource: "userDatabseFakeData", ofType: "json") {
        do {
            let jsonData = try NSData(contentsOfFile: path, options: NSData.ReadingOptions.mappedIfSafe)
            do {
                let jsonResult: NSDictionary = try JSONSerialization.jsonObject(with: jsonData as Data, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSDictionary
                if let people : [NSDictionary] = jsonResult["person"] as? [NSDictionary] {
                    for person: NSDictionary in people {
                        for (name,value) in person {
                            print("\(name) , \(value)")
                        }
                    }
                }
            } catch {}
        } catch {}
    }
Immaterial answered 25/9, 2016 at 15:33 Comment(0)
H
23

Simplifying the example provided by Peter Kreinz. Works with Swift 4.2.

The extension function:

extension Decodable {
  static func parse(jsonFile: String) -> Self? {
    guard let url = Bundle.main.url(forResource: jsonFile, withExtension: "json"),
          let data = try? Data(contentsOf: url),
          let output = try? JSONDecoder().decode(self, from: data)
        else {
      return nil
    }

    return output
  }
}

The example model:

struct Service: Decodable {
  let name: String
}

The example usage:

/// service.json
/// { "name": "Home & Garden" }

guard let output = Service.parse(jsonFile: "service") else {
// do something if parsing failed
 return
}

// use output if all good

The example will work with arrays, too:

/// services.json
/// [ { "name": "Home & Garden" } ]

guard let output = [Service].parse(jsonFile: "services") else {
// do something if parsing failed
 return
}

// use output if all good

Notice how we don't provide any unnecessary generics, thus we don't need to cast the result of parse.

Haddix answered 23/2, 2019 at 19:28 Comment(0)
S
14

Updated names for Swift 3.0

Based on Abhishek's answer and Druva's answer

func loadJson(forFilename fileName: String) -> NSDictionary? {

    if let url = Bundle.main.url(forResource: fileName, withExtension: "json") {
        if let data = NSData(contentsOf: url) {
            do {
                let dictionary = try JSONSerialization.jsonObject(with: data as Data, options: .allowFragments) as? NSDictionary

                return dictionary
            } catch {
                print("Error!! Unable to parse  \(fileName).json")
            }
        }
        print("Error!! Unable to load  \(fileName).json")
    }

    return nil
}
Smoothspoken answered 22/9, 2016 at 21:17 Comment(0)
A
10

Swift 2.1 answer (based on Abhishek's) :

    if let path = NSBundle.mainBundle().pathForResource("test", ofType: "json") {
        do {
            let jsonData = try NSData(contentsOfFile: path, options: NSDataReadingOptions.DataReadingMappedIfSafe)
            do {
                let jsonResult: NSDictionary = try NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
                if let people : [NSDictionary] = jsonResult["person"] as? [NSDictionary] {
                    for person: NSDictionary in people {
                        for (name,value) in person {
                            print("\(name) , \(value)")
                        }
                    }
                }
            } catch {}
        } catch {}
    }
Aerotherapeutics answered 8/11, 2015 at 22:43 Comment(0)
D
10

Swift 3.0, Xcode 8, iOS 10

 if let path = Bundle.main.url(forResource: "person", withExtension: "json") {

        do {
            let jsonData = try Data(contentsOf: path, options: .mappedIfSafe)
            do {
                if let jsonResult = try JSONSerialization.jsonObject(with: jsonData, options: JSONSerialization.ReadingOptions(rawValue: 0)) as? NSDictionary {
                    if let personArray = jsonResult.value(forKey: "person") as? NSArray {
                        for (_, element) in personArray.enumerated() {
                            if let element = element as? NSDictionary {
                                let name = element.value(forKey: "name") as! String
                                let age = element.value(forKey: "age") as! String
                                let employed = element.value(forKey: "employed") as! String
                                print("Name: \(name),  age: \(age), employed: \(employed)")
                            }
                        }
                    }
                }
            } catch let error as NSError {
                print("Error: \(error)")
            }
        } catch let error as NSError {
            print("Error: \(error)")
        }
    }

Output:

Name: Bob,  age: 16, employed: No
Name: Vinny,  age: 56, employed: Yes
Donnie answered 13/11, 2016 at 3:29 Comment(0)
S
8

One more answer here???

Ok. Hold on! All of the answers before were about using JSONSerialization, or returns nil, or ignores errors.

What is the different

"My solution" (is is not really my, this is a mix of the solutions above) contains:

  1. Modern way to return values: Result<Value,Error> (returns Value or Error)
  2. Avoids nil usage
  3. Contains a slightly verbose error
  4. Uses extension to have pretty/intuitive interface: Model.from(localJSON: "myJsonFile")
  5. Gives possibility to select bundle

Details

  • Xcode 14
  • Swift 5.6.1

Solution 1. JSON file -> Decodable

enum JSONParseError: Error {
    case fileNotFound
    case dataInitialisation(error: Error)
    case decoding(error: Error)
}

extension Decodable {
    static func from(localJSON filename: String,
                     bundle: Bundle = .main) -> Result<Self, JSONParseError> {
        guard let url = bundle.url(forResource: filename, withExtension: "json") else {
            return .failure(.fileNotFound)
        }
        let data: Data
        do {
            data = try Data(contentsOf: url)
        } catch let error {
            return .failure(.dataInitialisation(error: error))
        }

        do {
            return .success(try JSONDecoder().decode(self, from: data))
        } catch let error {
            return .failure(.decoding(error: error))
        }
    }
}

Solution 1 Usage

 struct Model: Decodable {
    let uuid: String
    let name: String
}

switch Model.from(localJSON: "myjsonfile") {
case .success(let value):
    print(value)
case .failure(let error):
    print(error)
}

Solution 2. JSON file -> Dictionary

extension Dictionary where Key == String, Value == Any {

    enum JSONParseError: Error {
        case fileNotFound(filename: String)
        case dataInitialisation(Error)
        case jsonSerialization(Error)
        case mappingFail(value: Any, toType: Any)
    }

    static func from(JSONfile url: URL) -> Result<Self, JSONParseError> {
        let data: Data
        do {
            data = try Data(contentsOf: url)
        } catch let error {
            return .failure(.dataInitialisation(error))
        }

        let jsonObject: Any
        do {
            jsonObject = try JSONSerialization.jsonObject(with: data, options: .mutableLeaves)
        } catch let error {
            return .failure(.jsonSerialization(error))
        }

        guard let jsonResult = jsonObject as? Self else {
            return .failure(.mappingFail(value: jsonObject, toType: Self.Type.self))
        }

        return .success(jsonResult)
    }

    static func from(localJSONfile name: String) -> Result<Self, JSONParseError> {
        let fileType = "json"
        let fullFileName = name + (name.contains(fileType) ? "" : ".\(fileType)")
        guard let path = Bundle.main.path(forResource: fullFileName, ofType: "") else {
            return .failure(.fileNotFound(filename: fullFileName))
        }
        return from(JSONfile: URL(fileURLWithPath: path))
    }
}

Solution 2 Usage

switch [String: Any].from(localJSONfile: "file.json") {
// OR switch [String: Any].from(localJSONfile: "file.json") {
// OR switch [String: Any].from(JSONfile: url) {
case let .success(dictionary):
    print(dictionary)
case let .failure(error):
    print("ERROR: \(error)")
}
Shimmer answered 20/9, 2022 at 15:58 Comment(0)
J
7

This worked great with me

func readjson(fileName: String) -> NSData{

    let path = NSBundle.mainBundle().pathForResource(fileName, ofType: "json")
    let jsonData = NSData(contentsOfMappedFile: path!)

    return jsonData!
}
Jigger answered 7/3, 2015 at 16:20 Comment(0)
T
7

Here is my solution using SwiftyJSON

if let path : String = NSBundle.mainBundle().pathForResource("filename", ofType: "json") {
    if let data = NSData(contentsOfFile: path) {

        let json = JSON(data: data)

    }
}
Thane answered 30/5, 2015 at 15:48 Comment(0)
C
7
fileprivate class BundleTargetingClass {}
func loadJSON<T>(name: String) -> T? {
  guard let filePath = Bundle(for: BundleTargetingClass.self).url(forResource: name, withExtension: "json") else {
    return nil
  }

  guard let jsonData = try? Data(contentsOf: filePath, options: .mappedIfSafe) else {
    return nil
  }

  guard let json = try? JSONSerialization.jsonObject(with: jsonData, options: .allowFragments) else {
    return nil
  }

  return json as? T
}

👆🏻 copy-paste ready, 3rd party framework independent solution.

usage 👇🏻

let json:[[String : AnyObject]] = loadJSON(name: "Stations")!

Cheep answered 2/2, 2017 at 22:27 Comment(1)
this worked for me. I needed to hard code a searchable list of drugs into an app. I got the json file from mySQL database. I dropped the json file into my XCODE project running above in viewDidLoad and bam I had my json dictionary !!!Lollis
W
7

Swift 4: Try out my solution:

test.json

{
    "person":[
        {
            "name": "Bob",
            "age": "16",
            "employed": "No"
        },
        {
            "name": "Vinny",
            "age": "56",
            "employed": "Yes"
        }
    ]
}

RequestCodable.swift

import Foundation

struct RequestCodable:Codable {
    let person:[PersonCodable]
}

PersonCodable.swift

import Foundation

struct PersonCodable:Codable {
    let name:String
    let age:String
    let employed:String
}

Decodable+FromJSON.swift

import Foundation

extension Decodable {

    static func fromJSON<T:Decodable>(_ fileName: String, fileExtension: String="json", bundle: Bundle = .main) throws -> T {
        guard let url = bundle.url(forResource: fileName, withExtension: fileExtension) else {
            throw NSError(domain: NSURLErrorDomain, code: NSURLErrorResourceUnavailable)
        }

        let data = try Data(contentsOf: url)

        return try JSONDecoder().decode(T.self, from: data)
    }
}

Example:

let result = RequestCodable.fromJSON("test") as RequestCodable?

result?.person.compactMap({ print($0) }) 

/*
PersonCodable(name: "Bob", age: "16", employed: "No")
PersonCodable(name: "Vinny", age: "56", employed: "Yes")
*/
Wanyen answered 4/1, 2019 at 11:3 Comment(2)
Your fromJSON extension function throws, yet in the example you call it without the try keyword. This code will not compile.Haddix
Also, for fromJSON you use an Decodable extension, yet you don't use any information from the Decodable type but provide additional (completely useless) generics.Haddix
B
7

Use this generic function

func readJSONFromFile<T: Decodable>(fileName: String, type: T.Type) -> T? {
    if let url = Bundle.main.url(forResource: fileName, withExtension: "json") {
        do {
            let data = try Data(contentsOf: url)
            let decoder = JSONDecoder()
            let jsonData = try decoder.decode(T.self, from: data)
            return jsonData
        } catch {
            print("error:\(error)")
        }
    }
    return nil
}

with this line of code:

let model = readJSONFromFile(fileName: "Model", type: Model.self)

for this type:

struct Model: Codable {
    let tall: Int
}
Bernitabernj answered 8/6, 2020 at 9:25 Comment(0)
T
6

The following code works for me. I am using Swift 5

let path = Bundle.main.path(forResource: "yourJSONfileName", ofType: "json")
var jsonData = try! String(contentsOfFile: path!).data(using: .utf8)!

Then, if your Person Struct (or Class) is Decodable (and also all of its properties), you can simply do:

let person = try! JSONDecoder().decode(Person.self, from: jsonData)

I avoided all the error handling code to make the code more legible.

Tokoloshe answered 17/2, 2020 at 20:28 Comment(0)
Z
5

I'm providing another answer because none of the ones here are geared toward loading the resource from the test bundle. If you are consuming a remote service that puts out JSON and want to unit test parsing the results without hitting the actual service, you take one or more responses and put them into files in the Tests folder in your project.

func testCanReadTestJSONFile() {
    let path = NSBundle(forClass: ForecastIOAdapterTests.self).pathForResource("ForecastIOSample", ofType: "json")
    if let jsonData = NSData(contentsOfFile:path!) {
        let json = JSON(data: jsonData)
        if let currentTemperature = json["currently"]["temperature"].double {
            println("json: \(json)")
            XCTAssertGreaterThan(currentTemperature, 0)
        }
    }
}

This also uses SwiftyJSON but the core logic of getting the test bundle and loading the file is the answer to the question.

Zest answered 27/5, 2015 at 5:5 Comment(0)
M
4

Updated for Swift 3 with safest way

    private func readLocalJsonFile() {

    if let urlPath = Bundle.main.url(forResource: "test", withExtension: "json") {

        do {
            let jsonData = try Data(contentsOf: urlPath, options: .mappedIfSafe)

            if let jsonDict = try JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers) as? [String: AnyObject] {

                if let personArray = jsonDict["person"] as? [[String: AnyObject]] {

                    for personDict in personArray {

                        for (key, value) in personDict {

                            print(key, value)
                        }
                        print("\n")
                    }
                }
            }
        }

        catch let jsonError {
            print(jsonError)
        }
    }
}

enter image description here

Michelinemichell answered 2/8, 2017 at 10:1 Comment(1)
I prefer this answer because it shows how to actually loop through and access each value. ThanksBlondie
Q
3

Latest swift 3.0 absolutely working

func loadJson(filename fileName: String) -> [String: AnyObject]?
{
    if let url = Bundle.main.url(forResource: fileName, withExtension: "json") 
{
      if let data = NSData(contentsOf: url) {
          do {
                    let object = try JSONSerialization.jsonObject(with: data as Data, options: .allowFragments)
                    if let dictionary = object as? [String: AnyObject] {
                        return dictionary
                    }
                } catch {
                    print("Error!! Unable to parse  \(fileName).json")
                }
            }
            print("Error!! Unable to load  \(fileName).json")
        }
        return nil
    }
Quarter answered 20/1, 2017 at 10:59 Comment(0)
P
3

Swift 4 JSON to Class with Decodable - for those who prefer classes

Define the classes as follows:

class People: Decodable {
  var person: [Person]?

  init(fileName : String){
    // url, data and jsonData should not be nil
    guard let url = Bundle.main.url(forResource: fileName, withExtension: "json") else { return }
    guard let data = try? Data(contentsOf: url) else { return }
    guard let jsonData = try? JSONDecoder().decode(People.self, from: data) else { return }

    // assigns the value to [person]
    person = jsonData.person
  }
}

class Person : Decodable {
  var name: String
  var age: String
  var employed: String
}

Usage, pretty abstract:

let people = People(fileName: "people")
let personArray = people.person

This allow methods for both People and Person classes, variables (attributes) and methods can also marked as private if needed.

Pantsuit answered 26/3, 2019 at 4:48 Comment(0)
E
2

Swift 5+

Decoding the jsonData with your Struct

if let jsonData = readFile(forName: <your file name>) {

do {
                let decodedData = try JSONDecoder().decode(<your struct name>.self, from: jsonData)
                return decodedData.<what you expect>
            } catch { print("JSON decode error") }
}

This will read the file and return jsonData

In case you are actually in another bundle (test for instance), use:
let bundlePath = Bundle(for: type(of: self)).path(forResource: name, ofType: "json")

private func readFile(forName name: String) -> Data? {
        do {

            if let bundlePath = Bundle.main.path(forResource: name, ofType: "json"),
                let jsonData = try String(contentsOfFile: bundlePath).data(using: .utf8) {
                return jsonData
            }
        } catch {
            print(error)
        }
        return nil
    }
Engineman answered 21/7, 2021 at 11:21 Comment(0)
D
2

I wasted my time in locating file which was located in my project with name Jsondata.json. But I weren't able to locate my File through code....

Solution: Make sure that your Jsondata.json file is added in Project> Build Phases> Copy Bundle Resources. Otherwise you wont be able to get file and Bundle.main.url(forResource: fileName, withExtension: "json") will give you nil always.

Detoxify answered 28/2, 2022 at 11:28 Comment(0)
V
2

Swift 5 answer worked for me, except that is missing that i must add a empty file, rename it as xxx.json after it works, and using generics.

func loadJson<T:Codable>(filename fileName: String) -> T? {
        if let url = Bundle.main.url(forResource: fileName, withExtension: "json") {
            do {
                let data = try Data(contentsOf: url)
                let decoder = JSONDecoder()
                return  try decoder.decode(T.self, from: data)
            } catch {
                print("error:\(error)")
            }
        }
        return nil
    }

code

Valdes answered 6/9, 2022 at 19:3 Comment(0)
D
1

Based on Abhishek's answer, for iOS 8 this would be:

let masterDataUrl: NSURL = NSBundle.mainBundle().URLForResource("masterdata", withExtension: "json")!
let jsonData: NSData = NSData(contentsOfURL: masterDataUrl)!
let jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(jsonData, options: nil, error: nil) as! NSDictionary
var persons : NSArray = jsonResult["person"] as! NSArray
Dibs answered 30/6, 2015 at 19:24 Comment(1)
Are you using Swift 2.0? Then yes, that would be the case. This was answered pre-2.0.Dibs
P
1

This worked for me with XCode 8.3.3

func fetchPersons(){

    if let pathURL = Bundle.main.url(forResource: "Person", withExtension: "json"){

        do {

            let jsonData = try Data(contentsOf: pathURL, options: .mappedIfSafe)

            let jsonResult = try JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers) as! [String: Any]
            if let persons = jsonResult["person"] as? [Any]{

                print(persons)
            }

        }catch(let error){
            print (error.localizedDescription)
        }
    }
}
Providing answered 3/9, 2017 at 19:14 Comment(0)
F
1

Swift 4.1 Updated Xcode 9.2

if let filePath = Bundle.main.path(forResource: "fileName", ofType: "json"), let data = NSData(contentsOfFile: filePath) {

     do {
      let json = try JSONSerialization.jsonObject(with: data as Data, options: JSONSerialization.ReadingOptions.allowFragments)        
        }
     catch {
                //Handle error
           }
 }
Frodeen answered 10/7, 2018 at 12:9 Comment(1)
There is nothing new, quite the contrary: Don't use NSData in Swift 3+, and .allowFragments is pointless in this case.Martines
T
1

First create a Struc codable like this:

  struct JuzgadosList : Codable {
    var CP : Int
    var TEL : String
    var LOCAL : String
    var ORGANO : String
    var DIR : String
}

Now declare the variable

 var jzdosList = [JuzgadosList]()

Read from main directory

func getJsonFromDirectory() {

        if let path = Bundle.main.path(forResource: "juzgados", ofType: "json") {
            do {
                let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .alwaysMapped)
                let jList = try JSONDecoder().decode([JuzgadosList].self, from: data)
                self.jzdosList = jList

                DispatchQueue.main.async() { () -> Void in
                    self.tableView.reloadData()
                }

            } catch let error {
                print("parse error: \(error.localizedDescription)")
            }
        } else {
            print("Invalid filename/path.")
        }
    }

Read from web

func getJsonFromUrl(){

        self.jzdosList.removeAll(keepingCapacity: false)

        print("Internet Connection Available!")

        guard let url = URL(string: "yourURL")  else { return }

        let request = URLRequest(url: url, cachePolicy: URLRequest.CachePolicy.reloadIgnoringLocalCacheData, timeoutInterval: 60.0)
        URLSession.shared.dataTask(with: request) { (data, response, err) in
            guard let data = data else { return }
            do {
                let jList = try JSONDecoder().decode([JuzgadosList].self, from: data)
                self.jzdosList = jList

                DispatchQueue.main.async() { () -> Void in
                    self.tableView.reloadData()
                }
            } catch let jsonErr {
                print("Error serializing json:", jsonErr)
            }
        }.resume()
    }
Tolan answered 20/10, 2018 at 10:1 Comment(0)
B
1
//change type based on your struct and right JSON file

let quoteData: [DataType] =
    load("file.json")

func load<T: Decodable>(_ filename: String, as type: T.Type = T.self) -> T {
    let data: Data

    guard let file = Bundle.main.url(forResource: filename, withExtension: nil)
        else {
            fatalError("Couldn't find \(filename) in main bundle.")
    }

    do {
        data = try Data(contentsOf: file)
    } catch {
        fatalError("Couldn't load \(filename) from main bundle:\n\(error)")
    }

    do {
        let decoder = JSONDecoder()
        return try decoder.decode(T.self, from: data)
    } catch {
        fatalError("Couldn't parse \(filename) as \(T.self):\n\(error)")
    }
}


Bowe answered 21/11, 2019 at 18:55 Comment(0)
H
1

A generic approach can be like that:

Create json file with Response class name string

struct Response: Codable,FileDecodable {
    typealias T = Self
    let names:[Data]
}
protocol FileDecodable{
   associatedtype T:Codable
   static func loadJson() ->T?
}

extension FileDecodable{
    static func loadJson() -> T? {
        let fileName = String(describing: T.self)
        if let url = Bundle.main.url(forResource: fileName, withExtension: "json")     {
            do {
                let data = try Data(contentsOf: url)
                let decoder = JSONDecoder()
                let jsonData = try decoder.decode(T.self, from: data)
                return jsonData
            } catch {
                print("error:\(error)")
            }
        }
        return nil
    }
}
Historical answered 20/10, 2022 at 10:51 Comment(0)
B
0

I’ve used below code to fetch JSON from FAQ-data.json file present in project directory .

I’m implementing in Xcode 7.3 using Swift.

     func fetchJSONContent() {
            if let path = NSBundle.mainBundle().pathForResource("FAQ-data", ofType: "json") {

                if let jsonData = NSData(contentsOfFile: path) {
                    do {
                        if let jsonResult: NSDictionary = try NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary {

                            if let responseParameter : NSDictionary = jsonResult["responseParameter"] as? NSDictionary {

                                if let response : NSArray = responseParameter["FAQ"] as? NSArray {
                                    responseFAQ = response
                                    print("response FAQ : \(response)")
                                }
                            }
                        }
                    }
                    catch { print("Error while parsing: \(error)") }
                }
            }
        }

override func viewWillAppear(animated: Bool) {
        fetchFAQContent()
    }

Structure of JSON file :

{
    "status": "00",
    "msg": "FAQ List ",
    "responseParameter": {
        "FAQ": [
            {                
                "question": “Question No.1 here”,
                "answer": “Answer goes here”,  
                "id": 1
            },
            {                
                "question": “Question No.2 here”,
                "answer": “Answer goes here”,
                "id": 2
            }
            . . .
        ]
    }
}
Bourgeois answered 27/7, 2016 at 12:58 Comment(0)
T
0

I might also recommend Ray Wenderlich's Swift JSON Tutorial (which also covers the awesome SwiftyJSON alternative, Gloss). An excerpt (which granted, by itself, does not fully answer the poster, but the added value of this answer is the link, so no -1's for that, please):

In Objective-C, parsing and deserializing JSON is fairly straightforward:

NSArray *json = [NSJSONSerialization JSONObjectWithData:JSONData
options:kNilOptions error:nil];
NSString *age = json[0][@"person"][@"age"];
NSLog(@"Dani's age is %@", age);

In Swift, parsing and deserializing JSON is a little more tedious due to Swift optionals and type-safety [but as] part of Swift 2.0 the guard statement was introduced to help get rid of nested if statements:

var json: Array!
do {
  json = try NSJSONSerialization.JSONObjectWithData(JSONData, options: NSJSONReadingOptions()) as? Array
} catch {
  print(error)
}

guard let item = json[0] as? [String: AnyObject],
  let person = item["person"] as? [String: AnyObject],
  let age = person["age"] as? Int else {
    return;
}
print("Dani's age is \(age)")

Of course, in XCode 8.x, you just double-tap the space bar and say "Hey, Siri, please deserialize this JSON for me in Swift 3.0 with space/tab-indents."

Tincher answered 1/9, 2016 at 17:26 Comment(0)
M
0

SWIFTYJSON VERSION SWIFT 3

func loadJson(fileName: String) -> JSON {

    var dataPath:JSON!

    if let path : String = Bundle.main.path(forResource: fileName, ofType: "json") {
        if let data = NSData(contentsOfFile: path) {
             dataPath = JSON(data: data as Data)
        }
    }
    return dataPath
}
Mckay answered 29/1, 2017 at 19:7 Comment(0)
K
0

After cleaning and polishing my code, I came to these two functions that you could just add to your project and use them very neat and fast to read from a json file and convert the data to whatever type you want it to be!

public func readDataRepresentationFromFile(resource: String, type: String) -> Data? {
    let filePath = Bundle.main.path(forResource: resource, ofType: type)
    
    if let path = filePath {
        let result = FileManager.default.contents(atPath: path)
        return result
    }
    return nil
}

and then with the help of this function you can convert your data to whatever type you want:

public func getObject<T: Codable>(of type: T.Type, from file: String) -> T?  {
    guard let data = readDataRepresentationFromFile(resource: file, type: "json") else {
        return nil
    }
    if let object = try? JSONDecoder().decode(type, from: data) {
        return object
    }
    return nil
}

Example of application of this code: call this functions wherever in your code, give it the name of your json file and thats all you need!

func getInputDataFromSomeJson(jsonFileName: String) -> YourReqiuredOutputType? {
    return getObject(of: YourReqiuredOutputType.self, from: jsonFileName)
}
Kain answered 8/7, 2021 at 10:40 Comment(0)
R
0

For those looking to print the JSON data in the debugger / lldb after Decodable has thrown an error try this.

po try! JSONSerialization.jsonObject(with: data!, options: .mutableLeaves) as? Dictionary<String, Any>

Roulette answered 9/8, 2021 at 15:32 Comment(1)
Or add this as an extension to Data: import Foundation extension Data { func printJSON() { let json = NSString(data: self, encoding: String.Encoding.utf8.rawValue) print(json!) } } then do: whatEverYourEncodedDataVariableIs.printJson() It's not appearing as the proper code editor for some reason :/.Incommensurable
O
0

For those who is looking for reusable function I made a class that responsible for JSON loading.

import Foundation

class JSONLoader {
    static func load<T: Decodable>(resource: String, type: T.Type) -> T {
        guard let file = Bundle.main.url(forResource: resource, withExtension: nil) else {
            fatalError("Couldn't find \(resource) in main bundle.")
        }
        let data: Data
        do {
            data = try Data(contentsOf: file)
        } catch {
            fatalError("Couldn't load \(resource) from main bundle:\n\(error)")
        }
        do {
            return try JSONDecoder().decode(type, from: data)
        } catch {
            fatalError("Couldn't parse \(resource) as \(T.self):\n\(error)")
        }
    }
    
    static func load<T: Decodable>(resource: String) -> T {
        load(resource: resource, type: T.self)
    }
}
// Usage:
let employee1 = JSONLoader.load("employee.json", Employee.self)
let employee2: Employee = JSONLoader.load("employee.json")
Osteen answered 11/10, 2022 at 17:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.