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
}
}
}