TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Problems figuring out how to define a Decode struct for a complex JSON

Forums > Swift

Hello!

Looking for advise after staring at this problem for half a day.

I have a complex JSON thats returned by an API and I need to create the decode Struct but i'm having difficulties.

I invested in Ducky as I thought that would generate the Struct for me, but the generated code is throwing a whole bunch of errors.

Below is the first 50 lines of the generate Struct. I'm getting the error: Type 'Ab.Result.Action' does not conform to protocol 'Decodable' on line 5 and then on every following Struct line. So i'm assuming that there is a core issue that needs to be fixed but I dont understand what the error means. Below is just a sample this goes of for another 1000 lines.

Also screenshot here:

https://imgur.com/a/wXWNvT3

Any advise greatly appriciated, I was kinda hoping Ducky would be me saviour, but in this case sadly not.

Thank you!!

import Foundation

struct Ab: Codable {
  struct Result: Codable {
    struct Action: Codable {
      struct Criterium: Codable {
        var name: String
        var relevance: String
        var verbose: String
        var searchEngine: String?

        private enum CodingKeys: String, CodingKey {
          case name
          case relevance
          case verbose
          case searchEngine = "search_engine"
        }
      }

      struct Metric: Codable {
        struct TotalURL: Codable {
          var count: Int
          var percentage: Double
          var percentageInScope: Double
          var previousCount: Any?
          var previousPercentageInScope: Any?
          var previousPercentage: Any?

          private enum CodingKeys: String, CodingKey {
            case count
            case percentage
            case percentageInScope = "percentage_in_scope"
            case previousCount = "previous_count"
            case previousPercentageInScope = "previous_percentage_in_scope"
            case previousPercentage = "previous_percentage"
          }
        }

        struct TotalUrlsInScope: Codable {
          var count: Int
          var percentage: Double
          var previousPercentage: Any?
          var previousCount: Any?

          private enum CodingKeys: String, CodingKey {
            case count
            case percentage
            case previousPercentage = "previous_percentage"
            case previousCount = "previous_count"
          }
        }

2      

Hi! I suspect the problem in this type of lines.

 var previousCount: Any?

I doubt that Any is encodable/decodable, well logically, how decoder is supposed to know how to process Any data type...

2      

@ygeras That seems to be it, thank you! Wonder why Ducky feels that Any should be included if it's nor Decodeable? Thanks again!

2      

When you pasted theJSON into ~Ducky~ those field had no data in them but had keys so it put Any there for you to change if you do not know then you should delete them or comment out then only use them once you find the types.

You also used CodingKeys I would change the option in Ducky's Property Naming Style to Snake to Carmel and then add

let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase

It make the struct less cluttered

2      

Thank you Nigel, i'll take that advise. I don't need the fields in question.

2      

Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.