How to decode variable object in variable array with jsonDecoder in swift

58 Views Asked by At

I am new in SWIFT. I have been fighting with swift for a long time to decode my custom JSON. But I couldn't.

This is my JSON : tablesArray is an array and it has more than one tables as jsonobject But These objects are variable but keys of objects are fixed. For instance; settingsTable can't be in tablesArray at next request, or notificationsTable can be added as extra.

{
    "result": true,
    "message": "allowed",
    "messageType": "nothing",
    "action": "get",
    "tablesArray": [
        {
            "settingsTable": [
                {
                    "id": "1",
                    "available": "true",
                    "forceToUpdate": "true",
                    "currentVersion": "1.5",
                    "versionMessage": "Process is completed"
                }
            ]
        },
        {
            "userTable": [
                {
                    "userId": "4",
                    "userDate": "2023-12-03 20:00:22",
                    "userNameSurname": "David ELI",
                    "userGender": "Male",
                    "userMailAddress": "[email protected]",
                    "userPhoneNumber": "00495770000000",
                    "userStatus": "active"
                }
            ]
        },
        {
            "memberTable": [
                {
                    "memberId": "123",
                    "memberDate": "2023-04-17 17:20:17",
                    "memberStatus": "active",
                    "memberStatusDescription": "Created by system",
                }
                        ]
        },
        {
            "personnelTable": [
                {
                    "personnelId": "31",
                    "personnelDate": "2023-04-17 17:20:17",
                    "personnelLevel": "Engineer",
                    "personnelStatus": "active",
                    "personnelStatusDescription": "Created by system",
                    "companyId": "1"
                }
            ]
        }
    ]
}
try JSONDecoder().decode(StructMain.self, from: data!)
struct StructMain: Decodable {
    let result: Bool //ok
    let message: String //ok
    let messageType: String //ok
    let action: String //ok
    let tablesArray: [Tables] //trouble
}
//This is my all tables but at some requests can't be all in tablesArray. i understand i must to use if condition or like that...

struct Tables: Decodable {
    let settingsTable: [SettingsTable]
    let userTable: [UserTable]
    let memberTable: [MemberTable]
    let personnelTable: [PersonnelTable]
    let notificationsTable: [NotificationsTable]
    let paymentsTable: [PaymentsTable]
    let debitsTable: [DebitsTable]
    let checksTable: [ChecksTable]
    let logsTable: [LogsTable]
    let qrsTable: [QrsTable]
}
//For settingsTable;

struct SettingsTable: Decodable {
    let id: String
    let available: String
    let forceToUpdate: String
    let currentVersion: String
    let versionMessage: String
}

//For others etc....

i tried all examples in stackoverflow but i can't find an example or question like my JSON struct.

How can i parse my variable json in swift. Can you help me?

Thank you...

0

There are 0 best solutions below