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

SOLVED: How do I decode this JSON?

Forums > Swift

I'm trying to decode the JSON feed of this site:

I've created structs for the JSON feed like this:

import Foundation

// MARK: - Welcome
struct Welcome: Codable {
    var lastUpdated, ttl: Int
    var data: DataClass

// MARK: - DataClass
struct DataClass: Codable {
    var stations: [Station]

// MARK: - Station
struct Station: Codable {
    var stationID, name, address: String
    var lat, lon: Double
    var capacity: Int

and a ContentView like this:

import SwiftUI

struct ContentView: View {

    @State private var stations = [Station]()

    var body: some View {
        NavigationView {
            List(stations, id: \.name) { station in
                VStack(alignment: .leading) {
                .navigationBarTitle("Bike Docker", displayMode: .large)
            .onAppear(perform: loadStationData)

    func loadStationData() {
        guard let url = URL(string: "") else {
            print("Invalid URL")

        let request = URLRequest(url: url)

        URLSession.shared.dataTask(with: request) { data, response, error in
            if let data = data {
                if let decodedResponse = try?
                    JSONDecoder().decode(Welcome.self, from: data) {
                    DispatchQueue.main.async {
                     let welcome = decodedResponse
                        self.stations =
            print("Fetch failed: \(error?.localizedDescription ?? "Unknown error.")")

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {

This always triggers the fatal error and never decodes = not sure what I am doing wrong.. would be immensely greatful for any help


You variable names in the structs must exactly match the names in the JSON. There is no stationID and there is no lastUpdated. station_id and last_updated is the way to go or you choose a decoding strategy.


If you want to make it easy to visualize the json you get you should try a safari extension. Either JSON peep or Simply JSON are pretty good. I prefer the 2nd, and both are free.

This will help you with identifying the details mentioned in the comment above. "Your properties need to be named exactly as they are in the JSON"


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.