0

I have some JSON that I need to parse and process within my tvOS app. I've tried using the website quicktype.io to convert the JSON to a struct, but it comes back with nothing. I think it's because of the layout of the JSON.

What would be the proper codable struct to handle this data?

{
   "bnn":[
      "prod",
      "staging"
   ],
   "bravo":[
      "prod"
   ],
   "comedy":[
      "prod"
   ],
   "cp24":[
      "prod"
   ],
   "crave":[
      "prod",
      "french"
   ],
   "ctv":[
      "prod",
      "prod-empty",
      "prod-broken",
      "chromecast-enabled",
      "chromecast-enabled-is-enabled-empty",
      "chromecast-enabled-some-text",
      "chromecast-enabled-configpath-wrongurl",
      "chromecast-disabled",
      "adobe-heartbeat-enabled-false",
      "adobe-heartbeat-enabled-true",
      "adobe-heartbeat-is-enabled-empty",
      "adobe-heartbeat-is-enabled-some-text",
      "ads-enabled-live-disable",
      "ads-enabled-live-empty",
      "ads-enabled-live-enable",
      "ads-enabled-live-some-text",
      "ads-enabled-vod-disable",
      "ads-enabled-vod-empty",
      "ads-enabled-vod-enable",
      "ads-enabled-vod-some-text",
      "chromecast-enabled-applicationid-empty",
      "chromecast-enabled-upnext-empty",
      "chromecast-enabled-wrong-applicationid",
      "chromecast-upnext-disabled",
      "chromecast-upnext-enabled",
      "chromecast-upnext-isenabled-empty",
      "chromecast-upnext-isenabled-some-text",
      "chromecast-upnext-namescape-some-text",
      "chromecast-upnext-namespace-empty",
      "closed-captioning-disabled",
      "closed-captioning-enabled",
      "closed-captioning-enabled-empty",
      "closed-captioning-enabled-some-text",
      "comscore-enabled-false",
      "comscore-is-enabled-empty",
      "comscore-is-enabled-some-text",
      "comscore-publisher-id-empty",
      "comscore-publisher-id-some-text",
      "comscore-secret-empty",
      "comscore-secret-some-text",
      "conviva-customer-key-empty",
      "conviva-customer-key-some-text",
      "conviva-enabled-empty",
      "conviva-enabled-false",
      "conviva-enabled-some-text",
      "conviva-enabled-true",
      "conviva-product-type-empty",
      "conviva-product-type-some-text",
      "conviva-url-empty",
      "conviva-url-some-text",
      "enable-endscreen-disabled",
      "enable-endscreen-empty",
      "enable-endscreen-some-text",
      "enable-episode-list-disabled",
      "enable-episode-list-empty",
      "enable-episode-list-enabled",
      "enable-episode-list-some-text",
      "enable-shareable-disabled",
      "enable-shareable-empty",
      "enable-shareable-enabled",
      "enable-shareable-some-text",
      "enable-upnext-disabled",
      "enable-upnext-empty",
      "enable-upnext-enabled",
      "enable-upnext-some-text",
      "endscreen-enabled",
      "krux-brand-empty",
      "krux-brand-some-text",
      "krux-id-empty",
      "krux-id-some-text",
      "krux-is-enabled-empty",
      "krux-is-enabled-false",
      "krux-is-enabled-some-text",
      "krux-is-enabled-true",
      "krux-type-empty",
      "krux-type-some-text",
      "language-empty",
      "language-enabled-en",
      "language-enabled-fr",
      "language-some-text",
      "location-services-disable",
      "location-services-empty",
      "location-services-enable",
      "location-services-some-text",
      "moat-enabled-false",
      "moat-enabled-true",
      "moat-html5partnercode-missing",
      "moat-html5partnercode-some-text",
      "moat-is-enabled-empty",
      "moat-is-enabled-some-text",
      "omniture-channel-empty",
      "omniture-channel-some-text",
      "omniture-enabled-empty",
      "omniture-enabled-false",
      "omniture-enabled-some-text",
      "omniture-enabled-true",
      "omniture-phonetrackingid-empty",
      "omniture-phonetrackingid-some-text",
      "omniture-server-empty",
      "omniture-server-some-text",
      "omniture-tablettrackingid-empty",
      "omniture-tablettrackingid-some-text",
      "omniture-video-player-name-empty",
      "omniture-video-player-name-some-text",
      "show-cellular-streaming-message-disabled",
      "show-cellular-streaming-message-empty",
      "show-cellular-streaming-message-enabled",
      "show-cellular-streaming-message-some-text",
      "upnext-duration-disabled",
      "upnext-duration-empty",
      "upnext-duration-enabled",
      "upnext-duration-some-text",
      "upnext-offset-disabled",
      "upnext-offset-empty",
      "upnext-offset-enabled",
      "upnext-offset-some-text",
      "video-wifi-or-wifi-and-cell-disabled",
      "video-wifi-or-wifi-and-cell-empty",
      "video-wifi-or-wifi-and-cell-enabled",
      "video-wifi-or-wifi-and-cell-some-text"
   ],
   "ctv_news":[
      "prod"
   ],
   "ctvhub":[
      "prod"
   ],
   "ctvcomedy":[
      "prod"
   ],
   "ctvdrama":[
      "prod"
   ],
   "dgo":[
      "prod"
   ],
   "e_brand":[
      "prod"
   ],
   "rds":[
      "prod"
   ],
   "se":[
      "prod"
   ],
   "snackable":[
      "prod"
   ],
   "space":[
      "prod"
   ],
   "tsn":[
      "prod"
   ],
   "vidiqa":[
      "prod"
   ],
   "vidiqa111":[
      "prod",
      "omniture-server-empty"
   ]
}

Please advise?

3
  • 1
    quicktype.io can help you. Commented Feb 24, 2020 at 17:09
  • It can't actually, that's what I indicated in my description... Commented Feb 24, 2020 at 17:10
  • 3
    It comes back with nothing is not true. The suggested type [String: [String]] is indeed the common denominator. Otherwise add a property for each key and declare it as [String] for example let bnn : [String] Commented Feb 24, 2020 at 17:14

1 Answer 1

3

I'm not going to type out the whole struct for you, but a start would be

struct MyData: Codable {
   let bnn: [String]
   let bravo: [String]
   let comedy: [String]
   let cp24: [String]
   let brave: [String]
   // and so on until
   let vidiqa111: [String]
}

and if those string values are fixed, you could define an enum for them:

enum StringValue: String {
   case prod
   case staging
   case french
   // ...
}

and change the struct's properties to be of type [StringValue].

If you're fine with using just strings in dictionaries/arrays, you can also simply use what quicktype.io suggests:

typealias MyData = [String: [String]]
let myData = try? JSONDecoder().decode(MyData.self, from: jsonData)
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, but I think all I need to do is to assign the data coming back to a dictionary where the key is a string and the value is an array of strings.
In that case, quicktype.io gave you precisely what you needed: [String: [String]].
Cool, thanks.. I think I was expecting it to create a struct..
Usually you do, and moving away from stringly-typed APIs is almost always a good idea, but using a simple typealias is certainly also possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.