ios – Elegant options to decoding a JSON nested array in swift utilizing decodable


Presently working with an API, and whereas I’ve efficiently gotten it to decode the total outcome, I’m solely within the Entities/identifier portion. Whereas I’ve gotten it working and get what I need/want I really feel like this may very well be finished higher and extra elegant and possibly in a single step. Any perception/ideas appreciated.

JSON returned from API:

 {
    "rely": 4596,
    "entities": [
        {
            "facet_ids": [
                "contact",
                "siftery",
                "investor",
                "ipqwery",
                "aberdeen",
                "apptopia",
                "semrush",
                "company",
                "rank",
                "builtwith",
                "bombora",
                "key_event"
            ],
            "identifier": {
                "uuid": "468bef9f-2f50-590e-6e78-62e3adb05aa1",
                "worth": "Citi",
                "image_id": "v1417152861/pdgwqt8ddecult5ktvdf.jpg",
                "permalink": "citigroup",
                "entity_def_id": "group"
            },
            "short_description": "Citigroup is a diversified monetary providers holding firm that gives numerous monetary services."
        },
        {
            "facet_ids": [
                "contact",
                "siftery",
                "investor",
                "apptopia",
                "semrush",
                "company",
                "rank",
                "builtwith",
                "key_event"
            ],
            "identifier": {
                "uuid": "031a344b-c2b9-e60b-d950-1ae062026fde",
                "worth": "Citi",
                "image_id": "yzlzhjqpparamrswaqa1",
                "permalink": "citi-2",
                "entity_def_id": "group"
            },
            "short_description": "CITi is an NPO supporting the ICT sector in Western Cape."
        },
        {
            "facet_ids": [
                "contact",
                "siftery",
                "semrush",
                "company",
                "rank",
                "builtwith",
                "bombora"
            ],
            "identifier": {
                "uuid": "7ce45379-957c-49c5-bca2-c9ffd521f7da",
                "worth": "CITI",
                "image_id": "qbkqndm7d0wgbogxjcrs",
                "permalink": "citi-f7da",
                "entity_def_id": "group"
            },
            "short_description": "CITI trusted gateway to project-based change experience that main organisations have to thrive, change and innovate."
        }
    ]
}

Structs:

    struct Entity: Decodable, Identifiable
{
    var id: String
    var companyName: String
    var permalink: String
    var imageID: String
    
    init(from entity: Entities.Entity) throws
    {
        self.id = entity.identifier?.uuid ?? ""
        self.companyName = entity.identifier?.worth ?? ""
        self.permalink = entity.identifier?.permalink ?? ""
        self.imageID = entity.identifier?.image_id ?? ""
    }
    
}

 struct Entities: Decodable
    {
    var rely:Int?
    var entities: [Entity]?
    
    struct Entity: Decodable
    {
        var facet_ids:[String]?
        var identifier:Identifier?
        var short_description:String?
    }
    
    struct Identifier:Decodable
    {
        var permalink:String?
        var worth:String?
        var image_id:String?
        var entity_def_id:String?
        var uuid:String?
    }
}

Name to decode:

        if let knowledge = knowledge{
        do {
            let businessEntities = strive decoder.decode(Entities.self, from: knowledge)
            
            let entities:[Entity] = strive businessEntities.entities!.compactMap{
                entity in
                do
                {
                    return strive Entity(from: entity)
                }
            }

Similar Posts

Leave a Reply

Your email address will not be published.