UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Codable structs - trying to understand what a Codable struct should look like for this JSON

Forums > Swift

Hi, I'lm trying to understand Codable Structs but i'm not getting far.

I have an API that returns the following JSON

"segments": {
        "flags": [],
        "values": [
            {
                "name": "name1",
                "field": "field1"
            },
            {
                "name": "name2",
                "field": "field2"
            },
            {
                "name": "name3",
                "field": "field3"
            },
            {
                "name": "name4",
                "field": "field4"
            },
            {
                "name": "name5",
                "field": "field5"
            },
            {
                "name": "name6",
                "field": "field6"
            },
            {
                "name": "name7",
                "field": "field7"
            }
        ],
        "date_created": "2023-01-10T17:41:06.022379+00:00",
        "rules_raw": "blablabla"

And i've figured out how to connect to the API. But i'm not sure what a Struct should look like to decode this JSON.

I've tried various attempts, my last one being

struct APIRootSegments: Codable {
    let results: [Results]
}
struct Results: Codable {
    let dimensions: [String]
}

... but i'm getting an error

app[50824:708939] Task <67678C7A-EB69-4C67-86DF-437E5302B36D>.<32> finished with error [-1103] Error Domain=NSURLErrorDomain Code=-1103 "resource exceeds maximum size" UserInfo={NSLocalizedDescription=resource exceeds maximum size

Can anyone advise how the struct should look to decode this JSON?

If it helps, i'm really only intersted in the field: "name"

Thank you for any advice!

2      

OK, making progress. It's a GET request and I understand that you don't need a HTTP body for GET requests. I'm now getting. 404 on my API request. Will continue to investigate.

2      

First get a JSON reader (I use Ducky Model Editor. I had to mess around with the json that you gave as it not a valid json and ended with this

{
    "segments": {
        "flags": [],
        "values": [
            {
                "name": "name1",
                "field": "field1"
            },
            {
                "name": "name2",
                "field": "field2"
            },
            {
                "name": "name3",
                "field": "field3"
            },
            {
                "name": "name4",
                "field": "field4"
            },
            {
                "name": "name5",
                "field": "field5"
            },
            {
                "name": "name6",
                "field": "field6"
            },
            {
                "name": "name7",
                "field": "field7"
            }
        ],
        "date_created": "2023-01-10T17:41:06.022379+00:00",
        "rules_raw": "blablabla"
    }
}

Once put into "Ducky" you get

struct APIRootSegments: Codable {
  let segments: Segment
}

struct Segment: Codable {
  let flags: [Any]
  let values: [Value]
  let dateCreated: Date
  let rulesRaw: String
}

struct Value: Codable {
  let name: String
  let field: String
}

A couple of things to note flags is empty array it will not parse so better to find out the type in it eg String, Int etc.(OR leave it out if not needed!)

Also when decoding Date you might read this post Converting a string to a date as it look like it got factional seconds. (OR leave it out if not needed!)

If you only want the name then

struct APIRootSegments: Codable {
  let segments: Segment
}

struct Segment: Codable {
  let values: [Value]
}

struct Value: Codable {
  let name: String
}

2      

thats super useful, i'll look into this right now. I didnt know about Ducky. If it's useful i'll be happy to purchase it. I'm finding it difficult to get my head arround how the structs map tO JSON so anything that can help in this regard is great. Thanks again!

2      

Ducky is Free

You might want to watch Ducky Model Editor for Swift Developers by Stewart Lynch to get started.

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.