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

How do I fix this JSON file so it works in my app?

Forums > SwiftUI

I am attempting to write a technical dictionary app and am basing it on the Moonshot app in 100 days of SwiftUI. My data structure is simple:

struct Definition: Codable, Identifiable {
    let id: Int
    let term: String
    let meaning: String
}

My test .json file attempts to work with that structure:

{
    1:
        {
            "id": 1
            "term": "a",
            "meaning": "The first letter of the alphabet."
        },
    2:
        {
            "id": 2
            "term": "bird",
            "meaning": "Birds are a group of warm-blooded vertebrates constituting the class Aves (/ˈeɪviːz/), characterised by feathers, toothless beaked jaws, the laying of hard-shelled eggs, a high metabolic rate, a four-chambered heart, and a strong yet lightweight skeleton. ."
        },
    3:
        {
            "id": 3
            "term": "football",
            "meaning": "Football is a family of team sports that involve, to varying degrees, kicking a ball to score a goal."
        }
}

The JSON file won't decode using:

let definitions: [Int: Definition] = Bundle.main.decode("Test.json")`

and I've tried many variations. It looks very similar to the astronauts.json file in the Moonshot app but something is clearly missing.

Any help is much appreciated.

Cheers Steve

3      

You cannot use numbers as object keys in JSON if you expect it to be valid. Only strings are allowed as keys.

You are missing commas after each of the id keys.

Also, I think you probably want an array here anyway.

So, something like this:

[
    {
        "id": 1,
        "term": "a",
        "meaning": "The first letter of the alphabet."
    },
    {
        "id": 2,
        "term": "bird",
        "meaning": "Birds are a group of warm-blooded vertebrates constituting the class Aves (/ˈeɪviːz/), characterised by feathers, toothless beaked jaws, the laying of hard-shelled eggs, a high metabolic rate, a four-chambered heart, and a strong yet lightweight skeleton. ."
    },
    {
        "id": 3,
        "term": "football",
        "meaning": "Football is a family of team sports that involve, to varying degrees, kicking a ball to score a goal."
    }
]

3      

Thanks roosterboy. I'll play around with your suggestions. I actually have a working solution using strings for keys but it is not as nice looking as the json files in the Moonshot example.

3      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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

Reply to this topic…

You need to create an account or log in to reply.

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.