json array shows in debugger but can't parse

Hello,

I have a json array showing in Xcode debugger (from the line "print(dataString)"):

Optional("[{\"id\":\"8e8tfdcssu4u2hn7a71tkveahjhn8xghqcfkwf1bzvtrw5nu0b89w\",\"name\":\"Ameliana\",\"country\":\"France\",\"type\":\"Private\\/Corporate\",\"overallrecsit\":\"Positive\",\"dlastupd\":\"1741351633\",\"doverallrecsit\":\"1546848000\"},{\"id\":\"z69718a1a5z2y5czkwrhr1u37h7h768v05qr3pf1fegh4r4yrt5a68\",\"name\":\"Timberland\",\"country\":\"Switzerland\",\"type\":\"Charter\",\"overallrecsit\":\"Negative\",\"dlastupd\":\"1741351615\",\"doverallrecsit\":\"1740434582\"},

But my JSON decoder is throwing the catch error "Error in JSON parsing"

This is the code:

        super.viewDidLoad()

        let urlString = "https://www.pilotjobsnetwork.com/service_ios.php"
        let url = URL(string: urlString)
        
        guard url != nil else {
            return
        }
        
        let session = URLSession.shared
        
        let dataTask = session.dataTask(with: url!) { (data, response, error) in
           
            var dataString = String(data: data!, encoding: String.Encoding.utf8)
            print(dataString)
            
            if error == nil && data != nil {
                // Parse JSON
                let decoder = JSONDecoder()
                
                do {
                    
                let newsFeed = try decoder.decode(NewsFeed.self, from: data!)
                    
                    print(newsFeed)
                    print(error)
                    
                }
                catch{
                    print("Error in JSON parsing")
                }
                
            }
        }
        
        // Make the API Call
        dataTask.resume()
    }

And this is my Codable file NewsFeed.swift:

    
    struct NewsFeed: Codable {
        
        var id: String
        var name: String
        var country: String
        var type: String
        var overallrecsit: String
        var dlastupd: String
        var doverallrecsit: String
        
    }

Please do you know why the parsing may be failing? Is it significant that in the debugging window the JSON is displaying backslashes before the quotation marks?

Thank you for any pointers :-)

Answered by DTS Engineer in 828360022

In situations like this I:

  1. Check whether the JSON itself is valid.

  2. Then put it into a small test program and debug from there.

The JSON you posted was truncated, but I grabbed what I could and run it through jq:

% cat test.json
[{"id":"8e8tfdcssu4u2hn7a71tkveahjhn8xghqcfkwf1bzvtrw5nu0b89w","name":"Ameliana","country":"France","type":"Private\\/Corporate","overallrecsit":"Positive","dlastupd":"1741351633","doverallrecsit":"1546848000"},{"id":"z69718a1a5z2y5czkwrhr1u37h7h768v05qr3pf1fegh4r4yrt5a68","name":"Timberland","country":"Switzerland","type":"Charter","overallrecsit":"Negative","dlastupd":"1741351615","doverallrecsit":"1740434582"}]
% jq . test.json 
[
  {
    "id": "8e8tfdcssu4u2hn7a71tkveahjhn8xghqcfkwf1bzvtrw5nu0b89w",
    "name": "Ameliana",
    "country": "France",
    "type": "Private\\/Corporate",
    "overallrecsit": "Positive",
    "dlastupd": "1741351633",
    "doverallrecsit": "1546848000"
  },
  {
    "id": "z69718a1a5z2y5czkwrhr1u37h7h768v05qr3pf1fegh4r4yrt5a68",
    "name": "Timberland",
    "country": "Switzerland",
    "type": "Charter",
    "overallrecsit": "Negative",
    "dlastupd": "1741351615",
    "doverallrecsit": "1740434582"
  }
]

So far so good. I then created this program:

import Foundation

let json = """
[{"id":"8e8tfdcssu4u2hn7a71tkveahjhn8xghqcfkwf1bzvtrw5nu0b89w","name":"Ameliana","country":"France","type":"Private\\/Corporate","overallrecsit":"Positive","dlastupd":"1741351633","doverallrecsit":"1546848000"},{"id":"z69718a1a5z2y5czkwrhr1u37h7h768v05qr3pf1fegh4r4yrt5a68","name":"Timberland","country":"Switzerland","type":"Charter","overallrecsit":"Negative","dlastupd":"1741351615","doverallrecsit":"1740434582"}]
"""

struct NewsFeed: Codable {
    var id: String
    var name: String
    var country: String
    var type: String
    var overallrecsit: String
    var dlastupd: String
    var doverallrecsit: String
}

func main() throws {
    let feeds = try JSONDecoder().decode([NewsFeed].self, from: Data(json.utf8))
    print(feeds)
}

try main()

It works as well, printing:

[xxst.NewsFeed(id: "8e8tfdcssu4u2hn7a71tkveahjhn8xghqcfkwf1bzvtrw5nu0b89w", name: "Ameliana", country: "France", type: "Private/Corporate", overallrecsit: "Positive", dlastupd: "1741351633", doverallrecsit: "1546848000"), xxst.NewsFeed(id: "z69718a1a5z2y5czkwrhr1u37h7h768v05qr3pf1fegh4r4yrt5a68", name: "Timberland", country: "Switzerland", type: "Charter", overallrecsit: "Negative", dlastupd: "1741351615", doverallrecsit: "1740434582")]

I’m not sure why this is failing in your real app, but it’s probably something to do with the JSON data that you elided. My advice is that follow the above process and see where that leads you.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

In situations like this I:

  1. Check whether the JSON itself is valid.

  2. Then put it into a small test program and debug from there.

The JSON you posted was truncated, but I grabbed what I could and run it through jq:

% cat test.json
[{"id":"8e8tfdcssu4u2hn7a71tkveahjhn8xghqcfkwf1bzvtrw5nu0b89w","name":"Ameliana","country":"France","type":"Private\\/Corporate","overallrecsit":"Positive","dlastupd":"1741351633","doverallrecsit":"1546848000"},{"id":"z69718a1a5z2y5czkwrhr1u37h7h768v05qr3pf1fegh4r4yrt5a68","name":"Timberland","country":"Switzerland","type":"Charter","overallrecsit":"Negative","dlastupd":"1741351615","doverallrecsit":"1740434582"}]
% jq . test.json 
[
  {
    "id": "8e8tfdcssu4u2hn7a71tkveahjhn8xghqcfkwf1bzvtrw5nu0b89w",
    "name": "Ameliana",
    "country": "France",
    "type": "Private\\/Corporate",
    "overallrecsit": "Positive",
    "dlastupd": "1741351633",
    "doverallrecsit": "1546848000"
  },
  {
    "id": "z69718a1a5z2y5czkwrhr1u37h7h768v05qr3pf1fegh4r4yrt5a68",
    "name": "Timberland",
    "country": "Switzerland",
    "type": "Charter",
    "overallrecsit": "Negative",
    "dlastupd": "1741351615",
    "doverallrecsit": "1740434582"
  }
]

So far so good. I then created this program:

import Foundation

let json = """
[{"id":"8e8tfdcssu4u2hn7a71tkveahjhn8xghqcfkwf1bzvtrw5nu0b89w","name":"Ameliana","country":"France","type":"Private\\/Corporate","overallrecsit":"Positive","dlastupd":"1741351633","doverallrecsit":"1546848000"},{"id":"z69718a1a5z2y5czkwrhr1u37h7h768v05qr3pf1fegh4r4yrt5a68","name":"Timberland","country":"Switzerland","type":"Charter","overallrecsit":"Negative","dlastupd":"1741351615","doverallrecsit":"1740434582"}]
"""

struct NewsFeed: Codable {
    var id: String
    var name: String
    var country: String
    var type: String
    var overallrecsit: String
    var dlastupd: String
    var doverallrecsit: String
}

func main() throws {
    let feeds = try JSONDecoder().decode([NewsFeed].self, from: Data(json.utf8))
    print(feeds)
}

try main()

It works as well, printing:

[xxst.NewsFeed(id: "8e8tfdcssu4u2hn7a71tkveahjhn8xghqcfkwf1bzvtrw5nu0b89w", name: "Ameliana", country: "France", type: "Private/Corporate", overallrecsit: "Positive", dlastupd: "1741351633", doverallrecsit: "1546848000"), xxst.NewsFeed(id: "z69718a1a5z2y5czkwrhr1u37h7h768v05qr3pf1fegh4r4yrt5a68", name: "Timberland", country: "Switzerland", type: "Charter", overallrecsit: "Negative", dlastupd: "1741351615", doverallrecsit: "1740434582")]

I’m not sure why this is failing in your real app, but it’s probably something to do with the JSON data that you elided. My advice is that follow the above process and see where that leads you.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

The example you posted works correctly for me. If things are failing for you then either the JSON data is malformed, or you’ve misunderstood its format and thus you need to tweak NewsFeed. Whatever’s going on is after the point that you truncated the JSON data, so it’s gonna be hard for anyone to help you.

So, I recommend that you pull this code out into a test project that reproduces the issue. That’ll either help you understand the issue, or you can share the test project so that others can take a look.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

json array shows in debugger but can't parse
 
 
Q