0

So I’m working on a personal project to analyze data from a json on my apple watch and im stuck on serializing the json. In some parts of the json, some keys are present and in others there’s keys missing. I’ve created a struct (below) to organize the information correctly but I want to be able to check that they are all there or add a key with a default value if it is missing.

public struct dataParameters: Encodable, Decodable{
     let key1: String
     let key2: String
     let key3: Int }

“dataParameters": [{“key1": "0", “key2": "0”}]

How can I check the keys present and then handle any missing one?

  • Does this answer your question? [With JSONDecoder in Swift 4, can missing keys use a default value instead of having to be optional properties?](https://stackoverflow.com/questions/44575293/with-jsondecoder-in-swift-4-can-missing-keys-use-a-default-value-instead-of-hav) – CRD May 15 '20 at 22:39
  • Yup this solved my problem thanks! – Ruben Zapata May 19 '20 at 18:44

0 Answers0