0

I need to get into logo - original - url. Having issues getting the syntax correct. here is the JSON. Im sure it is a small syntax error but help would be appreciated thanks. I think the issue is where i am created the struct for Logo - i feel like the "original" should be a dictionary, but i am getting an error when i do it both ways.

   {
    "pagination": {
        "page_number": 1, 
        "page_size": 50, 
        "continuation": "eyJwYWdlIjogMn0", 
        "has_more_items": true
    }, 
    "events": [
        {
            "name": {
                "text": "Atelier femmes entrepreneurs : Comprendre les leviers de la r\u00e9ussite", 
                "html": "Atelier femmes entrepreneurs : Comprendre les leviers de la r\u00e9ussite"
            }, 
            "description": {
                "text": "Matthieu Jardin offre aux femmes entrepreneurs un atelier d\u00e9velopper votre chiffre d\u2019affaires d\u2019une demi-journ\u00e9e.\nL\u2019atelier permet de d\u00e9couvrir des techniques marketing et commercial pour atteindre 150 000 \u20ac de chiffre d'affaire en 12 mois en ind\u00e9pendant (ou vous permettre de doubler ce chiffre d\u2019affaires si vous l\u2019avez d\u00e9j\u00e0 atteint).\nDepuis 10 ans, nous avons rencontr\u00e9 10 000 entrepreneurs permettant de comprendre les leviers de r\u00e9ussite d\u2019une activit\u00e9 professionnelle dans tous les domaines (sant\u00e9, beaut\u00e9, bien-\u00eatre, mieux-\u00eatre, m\u00e9decine douce, d\u00e9veloppement personnel, coaching, esth\u00e9tique, minceur, marketing, webmarketing, commercial, conseils, services, informatique, photographie, \u00e9dition, \u2026)Venez d\u00e9couvrir nos m\u00e9thodes \u00e0 succ\u00e8s, issue d\u2019une r\u00e9ussite manifeste puis\u00e9e au c\u0153ur de notre exp\u00e9rience.Sortez du lot mat\u00e9rialisez vos r\u00eaves et vibrez votre chiffre d\u2019affaires.", 
                "html": "<P>Matthieu Jardin offre aux <STRONG>femmes entrepreneurs<\/STRONG> un atelier d\u00e9velopper votre chiffre d\u2019affaires d\u2019une demi-journ\u00e9e.<\/P>\n<P>L\u2019atelier permet de d\u00e9couvrir des techniques marketing et commercial pour <STRONG>atteindre 150 000 \u20ac de chiffre d'affaire en 12 mois en ind\u00e9pendant<\/STRONG> (ou vous permettre de doubler ce chiffre d\u2019affaires si vous l\u2019avez d\u00e9j\u00e0 atteint).<\/P>\n<P><BR>Depuis 10 ans, nous avons rencontr\u00e9 <STRONG>10 000 entrepreneurs<\/STRONG> permettant de comprendre les leviers de r\u00e9ussite d\u2019une activit\u00e9 professionnelle dans tous les domaines (sant\u00e9, beaut\u00e9, bien-\u00eatre, mieux-\u00eatre, m\u00e9decine douce, d\u00e9veloppement personnel, coaching, esth\u00e9tique, minceur, marketing, webmarketing, commercial, conseils, services, informatique, photographie, \u00e9dition, \u2026)<BR><BR>Venez d\u00e9couvrir nos m\u00e9thodes \u00e0 succ\u00e8s, issue d\u2019une r\u00e9ussite manifeste puis\u00e9e au c\u0153ur de notre exp\u00e9rience.<BR><BR>Sortez du lot mat\u00e9rialisez vos r\u00eaves et <STRONG>vibrez votre chiffre d\u2019affaires.<\/STRONG><\/P>"
            }, 
            "id": "45831548395", 
            "url": "https://www.eventbrite.fr/e/billets-atelier-femmes-entrepreneurs-comprendre-les-leviers-de-la-reussite-45831548395", 
            "start": {
                "timezone": "Europe/Paris", 
                "local": "2018-12-20T10:00:00", 
                "utc": "2018-12-20T09:00:00Z"
            }, 
            "end": {
                "timezone": "Europe/Paris", 
                "local": "2018-12-20T12:00:00", 
                "utc": "2018-12-20T11:00:00Z"
            }, 
            "organization_id": "216426671512", 
            "created": "2018-05-05T21:03:44Z", 
            "changed": "2018-05-05T21:03:44Z", 
            "capacity": 500, 
            "capacity_is_custom": false, 
            "status": "live", 
            "currency": "EUR", 
            "listed": true, 
            "shareable": true, 
            "online_event": false, 
            "tx_time_limit": 480, 
            "hide_start_date": false, 
            "hide_end_date": false, 
            "locale": "fr_FR", 
            "is_locked": false, 
            "privacy_setting": "unlocked", 
            "is_series": true, 
            "is_series_parent": false, 
            "is_reserved_seating": false, 
            "show_pick_a_seat": false, 
            "show_seatmap_thumbnail": false, 
            "show_colors_in_seatmap_thumbnail": false, 
            "source": "create_2.0", 
            "is_free": true, 
            "version": "3.0.0", 
            "logo_id": "32865078", 
            "organizer_id": "14456692393", 
            "venue_id": "20195991", 
            "category_id": "101", 
            "subcategory_id": null, 
            "format_id": "9", 
            "resource_uri": "https://www.eventbriteapi.com/v3/events/45831548395/", 
            "series_id": "35835727626", 
            "logo": {
                "crop_mask": {
                    "top_left": {
                        "x": 0, 
                        "y": 0
                    }, 
                    "width": 2160, 
                    "height": 1080
                }, 
                "original": {
                    "url": "https://img.evbuc.com/https%3A%2F%2Fcdn.evbuc.com%2Fimages%2F32865078%2F216426671512%2F1%2Foriginal.jpg?auto=compress&s=90b9b081469d121ff3b446365c02865b", 
                    "width": 2160, 
                    "height": 1080
                }, 
                "id": "32865078", 
                "url": "https://img.evbuc.com/https%3A%2F%2Fcdn.evbuc.com%2Fimages%2F32865078%2F216426671512%2F1%2Foriginal.jpg?h=200&w=450&auto=compress&rect=0%2C0%2C2160%2C1080&s=9989b1febf7b1317555501973300a8f1", 
                "aspect_ratio": "2", 
                "edge_color": null, 
                "edge_color_set": true
            }
        },

Here is my Decodable code

struct Eventbrite: Decodable {
    let events: [Event]

}

struct Event: Decodable {
    let name: Name
    let description: Description
    let start: Start
    let logo: Logo
}

struct Name : Decodable {
    let text, html : String
}

struct Description : Decodable {
    let text : String?
}

struct Start : Decodable {
    let timezone, local, utc : String

}

struct Logo: Decodable{
    let original: String?
}

struct Original: Decodable{
    let url: String?
}
5
  • The JSON is not valid, at the end of the text line in the description dictionary a double quote is missing. Yes, original is a dictionary which is represented by a struct. It's the same pattern as the other dictionary / struct relationships. Hint: original is not String (and it's not optional). Commented May 6, 2018 at 4:59
  • Sorry- I actually edited and cut some of the in between JSON because it was like another 20 lines until the line I needed to decode came. So please excuse any of that Commented May 6, 2018 at 5:00
  • With the given incomplete and corrupted JSON nobody can quickly check the code in a Playground. Commented May 6, 2018 at 5:13
  • WOW! I had no clue! - i just updated the corrupted JSON now.. rookie mistake! apologies everybody Commented May 6, 2018 at 5:15
  • Is the JSON valid now ? Commented May 6, 2018 at 5:24

2 Answers 2

1

If you only need logo - original - url this is sufficient

struct Eventbrite: Decodable {
    let events: [Event]
}

struct Event: Decodable {
    let logo: Logo
}

struct Logo: Decodable {
    let original: Original // note that the type is not `String`
}

struct Original: Decodable {
    let url: URL
}

And print the URLs in all events with

let result = try JSONDecoder().decode(Eventbrite.self, from: data)
for event in result.events {
    print(event.logo.original.url)
}

It seems that you still don't understand the relationship dictionary <-> struct. You are encouraged to learn it 😉

And declare struct members only as optional if you get appropriate error messages.

Sign up to request clarification or add additional context in comments.

1 Comment

This worked- thank you. After about 4 hours of reading Apple documentation & watching tutorials. I understand better now lol! Thank you
0

Change this

struct Logo: Decodable{
    let original: String?
}

to

struct Logo: Decodable{
    let original: Original?
}

Here is full Code

import Foundation

struct Eventbrite: Codable {
    let events: [Event]
}

// MARK: Convenience initializers

extension Eventbrite {
    init(data: Data) throws {
        self = try JSONDecoder().decode(Eventbrite.self, from: data)
    }
}


struct Event: Codable {
    let name, description: Description
    let start : Start
    let logo: Logo

    enum CodingKeys: String, CodingKey {
        case name, description, logo, start
    }
}

struct Description: Codable {
    let text, html: String
}

struct Start: Codable {
    let timezone, local, utc: String
}

struct Logo: Codable {
    let cropMask: CropMask
    let original: Original
    let id, url, aspectRatio: String
    let edgeColorSet: Bool

    enum CodingKeys: String, CodingKey {
        case cropMask = "crop_mask"
        case original, id, url
        case aspectRatio = "aspect_ratio"
        case edgeColorSet = "edge_color_set"
    }
}

struct CropMask: Codable {
    let topLeft: TopLeft
    let width, height: Int

    enum CodingKeys: String, CodingKey {
        case topLeft = "top_left"
        case width, height
    }
}

struct TopLeft: Codable {
    let x, y: Int
}

struct Original: Codable {
    let url: String
    let width, height: Int
}

10 Comments

Hi, this game me an error - "Cannot get keyed decoding container -- found null value instead.", underlyingError: nil))
Thank you, I am plugging it in now to check. Is it necessary to have all of the structs such as topleft and cropMask? I only needed the image URL to display.
I don’t understand ? I’m sorry. Write in coding key where?
see my solution and check enum CodingKeys that allow you to to set your key as CamelCase , i use Coding Protocol which is Decoding and encoding
for more info about Codable you can read this hackernoon.com/everything-about-codable-in-swift-4-97d0e18a2999
|

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.