Converting Swift to JSON

I have a JSON object directly below that uses everything from Strings, Bools and Int's. This is an object I am trying to recreate in Xcode playground console. I'm currently having a difficult time recreating the person_details section of the object and I think because it's in brackets and has multiple values, like [String: Bool], [String: String] & [String: Int] ?

I posted towards the bottom what populates on the console, but any help structuring there person_details section in the would be great.

You'll see below, in my let order, I'm structuring the data.

If you need any additional info, please let me know. Thanks!

let testJson = """
{
"household": {
    "region": "PA",
    "household_size": 1,
    "receiving_benefits": [
    ],
    "energy_crisis": false,
    "utility_providers": [
        "peco"
    ],
    "residence_type": "other",
    "property_tax_past_due": false,
    "home_needs_repairs": false,
    "filed_previous_year_tax_return": false,
    "heating_system_needs_repairs": false,
    "at_risk_of_homelessness": false,
    "received_maximum_benefit": {
        "cip": false
    },
    "person_details": [
        {
            "age": 18,
            "marital_status": "single",
            "minimum_employment_over_extended_period": false,
            "work_status": "recent_loss",
            "pregnant": false,
            "attending_school": false,
            "disabled": false
        }
    ],
    "incomes": [
        {
            "*****_monthly_amount": 700,
            "countable_group": "household",
            "year": "current"
        },
        {
            "*****_monthly_amount": 700,
            "countable_group": "household",
            "year": "previous"
        }
    ],
    "assets": [
        {
            "amount": 1000,
            "countable_group": "household"
        }
    ]
}
}
"""

struct Eligibility: Encodable {
    let residence: String
    let hhmembers: Int
    let receivingBen: [String]
    let unhoused: Bool
    let utilityType: [String]
    let residenceType: String
    let propertyTax: Bool
    let homeRepairs: Bool
    let fileLastTax: Bool
    let heatRepairs: Bool
    let receivingMax: [String: Bool]
  //  let personDetails: [String]
  //  let marital1: String
  //  let age1: Int
 //   let pregnant: Bool

        enum CodingKeys: String, CodingKey {
            case residence = "region"
            case hhmembers = "household_size"
            case receivingBen = "receiving_benefits"
            case unhoused = "at_risk_of_homelessness"
            case utilityType = "utility_providers"
            case residenceType = "residence_type"
            case propertyTax = "property_tax_past_due"
            case homeRepairs = "home_needs_repairs"
            case fileLastTax = "filed_previous_year_tax_return"
            case heatRepairs = "heating_system_needs_repairs"
            case receivingMax = "received_maximum_benefit"
          //  case personDetails = "person_details"
         //   case marital1 = "marital_status"
         //   case age1 = "age"
         //   case pregnant = "pregnant"
        }
    }

struct Order: Encodable {
    let household: Eligibility
}

let order = Order(household: Eligibility(residence: "PA", hhmembers: 1, receivingBen: [], unhoused: false, utilityType: ["Peco"], residenceType: "other", propertyTax: false, homeRepairs: false, fileLastTax: false, heatRepairs: false, receivingMax: ["cip": false]))

let encoder = JSONEncoder()

encoder.outputFormatting = .prettyPrinted

let orderJsonData = try! encoder.encode(order)

print(String(data: orderJsonData, encoding: .utf8)!)

Console:

{
  "household" : {
    "region" : "PA",
    "residence_type" : "other",
    "at_risk_of_homelessness" : false,
    "property_tax_past_due" : false,
    "utility_providers" : [
      "Peco"
    ],
    "home_needs_repairs" : false,
    "filed_previous_year_tax_return" : false,
    "household_size" : 1,
    "receiving_benefits" : [
    ],
    "heating_system_needs_repairs" : false,
    "received_maximum_benefit" : {
      "cip" : false
    }
  }
}

You should create a struct for personDetails

struct PersonDetails: Encodable {
            let age: Int,
             let marital_status: String,
             let minimum_employment_over_extended_period: Bool,
             let work_status: String,
             let pregnant: Bool,
             let attending_school: Bool,
             let disabled: Bool
}

and use

  let personDetails: PersonDetails

You may have to define some more codingKeys.

@Claude31 I'm able to get person_details on the object but seems like it's not under the 'household'. The 'person_details' should be under 'household'. I have a strong suspicion it's something I need to do inside let order =.

This is what I have so far:

struct Order: Encodable {
    let household: Eligibility
    let person_details: PersonDetails
}

let order = Order(household: Eligibility(residence: "PA", hhmembers: 1, receivingBen: [], unhoused: false, utilityType: ["Peco"], residenceType: "other", propertyTax: false, homeRepairs: false, fileLastTax: false, heatRepairs: false, receivingMax: ["cip": false]), person_details: PersonDetails(age: 19))

As mentioned, it's not in the household object which I'm trying to get it under.

{
  "household" : {
    "region" : "PA",
    "residence_type" : "other",
    "at_risk_of_homelessness" : false,
    "property_tax_past_due" : false,
    "utility_providers" : [
      "Peco"
    ],
    "home_needs_repairs" : false,
    "filed_previous_year_tax_return" : false,
    "household_size" : 1,
    "receiving_benefits" : [
    ],
    "heating_system_needs_repairs" : false,
    "received_maximum_benefit" : {
      "cip" : false
    }
  },
  "person_details" : {
    "age" : 19
  }
}

It should look like this: (note the square brackets in person_details too)

`"household": {
    "region": "PA",
    "household_size": 1,
    "receiving_benefits": [
    ],
    "energy_crisis": false,
    "utility_providers": [
        "peco"
    ],
    "residence_type": "other",
    "property_tax_past_due": false,
    "home_needs_repairs": false,
    "filed_previous_year_tax_return": false,
    "heating_system_needs_repairs": false,
    "at_risk_of_homelessness": false,
    "received_maximum_benefit": {
        "cip": false
    },
    "person_details": [
        {
            "age": 18,
            "marital_status": "single",
            "minimum_employment_over_extended_period": false,
            "work_status": "recent_loss",
            "pregnant": false,
            "attending_school": false,
            "disabled": false
        }
    ],`

You should define a Codable struct for it too:

Would be like this:

struct Household: Encodable {
  var region : String,
  var household_size : Int,
  var receiving_benefits :  [String],     // Some array, but cannot find of what. String ?
  var energy_crisis : Bool,
  var utility_providers :  [String],
  var residence_type : String,
  var property_tax_past_due : Bool,
  var home_needs_repairs : Bool,
  var filed_previous_year_tax_return : Bool,
  var heating_system_needs_repairs : Bool,
  var at_risk_of_homelessness : Bool,
  var received_maximum_benefit : [String: Bool]
}
Converting Swift to JSON
 
 
Q