GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

Decode JSON with results from a Stored Procedure

Forums > Swift

I am changing my results from an API call from SQL to the results from a MariaDB Stored Procedure. The resulting JSON is below but I don't know how to set up my Structs properly ( I'm guessing) and then the decode itself isn't correct.

Sorry but I've read and read and strugle to understand how to correct this.

I have the following:

JSON

[ [ { "profile_id": 1, "lost_id": 14, "city": "", "state": "", "zip": "", "quick_desc": "", "comments": "", "longitude": "-122.40641700000002", "latitude": "37.78583399999998", "contact_phone": "", "image_id_1": 13, "image_id_2": 0, "image_id_3": 0, "last_update_TS": "2024-08-25T14:49:16.000Z", "location_desc": "", "lost_datetime": "2024-08-25T14:49:16.000Z", "status": "L", "status_desc": "Lost", "gender": "M", "neutered": "N", "userid": "kathylindsay", "radius": 2000, "distance": 1419.8404308667655 }, { "profile_id": 1, "lost_id": 15, "city": "", "state": "", "zip": "", "quick_desc": "", "comments": "", "longitude": "-122.40641700000002", "latitude": "37.78583399999998", "contact_phone": "", "image_id_1": 14, "image_id_2": 0, "image_id_3": 0, "last_update_TS": "2024-08-25T14:51:31.000Z", "location_desc": "", "lost_datetime": "2024-08-25T14:51:31.000Z", "status": "L", "status_desc": "Lost", "gender": "M", "neutered": "N", "userid": "kathylindsay", "radius": 2000, "distance": 1419.8404308667655 } ], { "affectedRows": 0, "insertId": 0, "warningStatus": 0 } ]

Structs

struct SPResults: Codable { var returned: [LPResult] var affectedRows: Int var insert_id: Int var err: Int }

struct LPResponse: Codable { var results: [LPResult] }

struct LPResult: Codable { var profile_id: Int var lost_id: Int var city: String var state: String var zip: String var quick_desc: String var comments: String var longitude: String var latitude: String var location_desc: String var contact_phone: String var image_id_1: Int var image_id_2: Int var image_id_3: Int var status: String var status_desc: String var gender: String var neutered:String var lost_datetime: String var userid: String var radius: Int var last_update_TS: String var distance: Double }

Code that throws and error of:

Error typeMismatch(Swift.Dictionary<Swift.String, Any>, Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription: "Expected to decode Dictionary<String, Any> but found an array instead.", underlyingError: nil))

Code: let url: URL = URL(string: urlPath)! // let defaultSession = Foundation.URLSession(configuration: URLSessionConfiguration.default)

           //let task = defaultSession.dataTask(with: url) { (data, response, error) in
           URLSession.shared.dataTask(with: url) { (data, response,err) in
                   guard let data = data else { return }
                   do {
                       let decodedResponse = try JSONDecoder().decode([SPResults].self, from: data)
                 //     let decodedResponse = try? JSONDecoder().decode(Response.self, from: data) {

                    DispatchQueue.main.async {
                        print(data)

                        // update our UI
                        //self.profile_ID = decodedResponse.LPResult[]
                        print(decodedResponse)

                    }
                    return
                   } catch let jsonErr {
                       print("Error", jsonErr)
               }
               }.resume()

Any help/direction to resolve would really be appreciated.

Thanks - Allan

   

Oh boy....I even suck at formatting a proper question on this forum - so sorry

   

I figured out how to get just the data in the JSON and not receive the OK PACKET that was causing me the problems so this isnt a problem that is stopping me from developing.

I do want to get better at JSON and how to properly set up/ work within Swift...and commentas are appreciated

   

Oh boy....I even suck at formatting a proper question on this forum - so sorry

Surround your code with backticks to make it format properly on the forum.

You can use a single backtick for one line of code or three backticks for multiple lines, like so:

proper backtick usage

   

Hacking with Swift is sponsored by Alex.

SPONSORED Alex is the iOS & Mac developer’s ultimate AI assistant. It integrates with Xcode, offering a best-in-class Swift coding agent. Generate modern SwiftUI from images. Fast-apply suggestions from Claude 3.5 Sonnet, o3-mini, and DeepSeek R1. Autofix Swift 6 errors and warnings. And so much more. Start your 7-day free trial today!

Try for free!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.