Model and Parse JSON Data

Hi @Chris2 and CodeCrew,

How would you guys model and parse the following json data?

The problem is: inside the Assuntos object, sometimes I have a string (1st object) and others I have an array (2nd object) in the field called Assunto.

With another json file, which contains only a string at Assunto, my app runs properly.

I’ve tried the Any data type, but Xcode gives me an error: Type ‘Documents’ does not conform to protocol ‘Decodable’

JSON data:

[{
“Categoria”: “Assembleia”,
“DataRef”: “11/06/2021 09:00:59”,
“Documento”: “IPE”,
“Especie”: “Ata”,
“FrmDtRef”: “dd/mm/aaaa hh:mm”,
“Situacao”: “Liberado”,
“Tipo”: “AGDEB”,
“ccvm”: “24287”,
“url”: “http://siteempresas.bovespa.com.br/DWL/FormDetalheDownload.asp?site=C&prot=877371”,
“Assuntos”: {
“Quantidade”: “1”,
“Assunto”: “Cancelamento de registro categoria B.”
}
},
{
“Categoria”: “Assembleia”,
“DataRef”: “30/04/2021 14:30:59”,
“Documento”: “IPE”,
“Especie”: “Ata”,
“FrmDtRef”: “dd/mm/aaaa hh:mm”,
“Situacao”: “Liberado”,
“Tipo”: “AGO”,
“ccvm”: “21970”,
“url”: “http://siteempresas.bovespa.com.br/DWL/FormDetalheDownload.asp?site=C&prot=877372”,
“Assuntos”: {
“Quantidade”: “6”,
“Assunto”: [
“Alteração endereço sede”,
“Aprovar a celebração da Apólice Seguro Garantia nº 017412021000107750030021, celebrado em 09 de fevereiro de 2021, com a BMG Seguros S.A., no valor de R$ 73.489.861,39, em favor da Agência Nacional de”,
“Destinação dos Resultados”,
“Eleição de Membros dos Conselhos de Administração e Fiscal”,
“Exame, discussão e aprovação do relatório de Administração, das contas da Diretoria, bem como das demonstrações financeiras da Companhia referentes ao exercício social encerrado em 31 de dezembro de 2”,
“Remuneração dos Administradores e Conselheiros”
]
}
}
]

My model (Documents.swift) file is:

//
// Documents.swift
// TesteJSON
//
// Created by Rafael Rodrigues on 17/06/21.
//

import Foundation

class Documents: Identifiable, Decodable {

var id: UUID?
var categoria: String?
var dataRef: String?
var documento: String?
var especie: String?
var formatoDataRef: String?
var situacao: String?
var tipo: String?
var ccvm: String?
var url: URL?
var assuntos: [String:String]?

enum CodingKeys: String, CodingKey {
    
    case id
    case categoria = "Categoria"
    case dataRef = "DataRef"
    case documento = "Documento"
    case especie = "Especie"
    case formatoDataRef = "FrmDtRef"
    case situacao = "Situacao"
    case tipo = "Tipo"
    case ccvm
    case url
    case assuntos = "Assuntos"
    
}

}

class Assuntos: Identifiable, Decodable {

var id: UUID?
var quantidade: String?
var assunto: [String]?

enum CodingKeys: String, CodingKey {

    case id
    case quantidade = "Quantidade"
    case assunto = "Assunto"
}

}

Thank you for your help.

Rafael

Actually I found the answer here: Understanding Type Erasure in Swift | by Mohammad Azam | Medium

On the face of it there is a problem with your JSON example. In the first array item you have “Assunto” as a String and in the second “Assunto” is an array of String. Is this how it is supposed to be?

If your JSON code was like this then it would make more sense and be easier to deal with from a Decoding point of view:

[
    {
        "Categoria": "Assembleia",
        "DataRef": "11/06/2021 09:00:59",
        "Documento": "IPE",
        "Especie": "Ata",
        "FrmDtRef": "dd/mm/aaaa hh:mm",
        "Situacao": "Liberado",
        "Tipo": "AGDEB",
        "ccvm": "24287",
        "url": "http://siteempresas.bovespa.com.br/DWL/FormDetalheDownload.asp?site=C&prot=877371",
        "Assuntos": {
            "Quantidade": "1",
            "Assunto":  [
                "Cancelamento de registro categoria B."
            ]
        }
    },
    {
        "Categoria": "Assembleia",
        "DataRef": "30/04/2021 14:30:59",
        "Documento": "IPE",
        "Especie": "Ata",
        "FrmDtRef": "dd/mm/aaaa hh:mm",
        "Situacao": "Liberado",
        "Tipo": "AGO",
        "ccvm": "21970",
        "url": "http://siteempresas.bovespa.com.br/DWL/FormDetalheDownload.asp?site=C&prot=877372",
        "Assuntos": {
            "Quantidade": "6",
            "Assunto": [
                "Alteração endereço sede",
                "Aprovar a celebração da Apólice Seguro Garantia nº 017412021000107750030021, celebrado em 09 de fevereiro de 2021, com a BMG Seguros S.A., no valor de R$ 73.489.861,39, em favor da Agência Nacional de",
                "Destinação dos Resultados",
                "Eleição de Membros dos Conselhos de Administração e Fiscal",
                "Exame, discussão e aprovação do relatório de Administração, das contas da Diretoria, bem como das demonstrações financeiras da Companhia referentes ao exercício social encerrado em 31 de dezembro de 2",
                "Remuneração dos Administradores e Conselheiros"
            ]
        }
    }
]

Hi @Chris_Parker ,

The JSON data comes from an API, and unfortunately it is what it is. But I found the solution using the AnyDecodable struct from the medium article. It works like a charm.

No worries.