0

I have following raw data to be sent as parameters while using alamofire in swift

{
    "customer": {
        "firstname": "test",
        "lastname": "user",
        "email": "testuser30@gmail.com",
        "website_id": 1,
        "addresses": [
        {
            "customer_id": 3292,
            "region": {
                "region": "New York"
            },
            "country_id": "US",
            "street": [
                "US"
            ],
            "telephone": "84656313",
            "postcode": "34521",
            "city": "us",
            "firstname": "test",
            "lastname": "usr",
            "default_shipping": true,
            "default_billing": true
        }
    ]

    }

}

I have written the parameters as given below in the code using alamofire.

  let customer : Parameters = [
                    "email":email,
                    "firstname":fname,
                    "lastname":lname,
                    "website_id":1,
                    "addresses": {
                        [
                            "customer_id": id,
                            "region": [
                                "region": state
                            ],
                            "country_id": country,
                            "street": {
                                self.add1Txt.text! + self.add2Txt.text!
                            },
                            "telephone": self.phoneTxt.text!,
                            "postcode": self.pincodeTxt.text!,
                            "city": self.cityTxt.text!,
                            "firstname": self.fnameLbl.text!,
                            "lastname": self.lnameLbl.text!,
                            "default_shipping": true,
                            "default_billing": true
                        ]
                    }
                 ]

                let parameters: Parameters =
                               [
                                    "customer": customer

                               ]

It shows 'Invalid type in JSON write (__SwiftValue)'. What is the issue with this parameter passing?

Saranya
  • 595
  • 2
  • 7
  • 19

2 Answers2

0

Your customer variable is not the correct dictionary. "addresses": { is actually an array of a dictionary. it should look like the following code. Other than this, you can avoid this kind of problem if you create model structures with Codable and take the help of JSONEncoder and JSONSerialization to convert to a dictionary. you can see this thread.

 let parameters: Parameters = ["customer": [
            "addresses" : [[

                            "city": "us",
                            "country_id" :"US",
                            "customer_id" :"3292",
                            "default_billing" : 1,
                            "default_shipping" : 1,
                            "firstname" :"test",
                            "lastname" :"usr",
                            "postcode" :34521,
                            "region" : ["region" : "New York"],
                            "street" : ["US"],
                            "telephone" : 84656313

            ]],
            "email" : "testuser30@gmail.com",
            "firstname": "test",
            "lastname": "user",
            "website_id" : 1
        ]]
Razib Mollick
  • 4,617
  • 2
  • 22
  • 20
0

Use Quicktype to instantly generate structs from your json:

import Foundation

// MARK: - Parameters
struct Parameters: Codable {
    let customer: Customer?
}

// MARK: - Customer
struct Customer: Codable {
    let firstname, lastname, email: String?
    let websiteID: Int?
    let addresses: [Address]?

    enum CodingKeys: String, CodingKey {
        case firstname, lastname, email
        case websiteID = "website_id"
        case addresses
    }
}

// MARK: - Address
struct Address: Codable {
    let customerID: Int?
    let region: Region?
    let countryID: String?
    let street: [String]?
    let telephone, postcode, city, firstname: String?
    let lastname: String?
    let defaultShipping, defaultBilling: Bool?

    enum CodingKeys: String, CodingKey {
        case customerID = "customer_id"
        case region
        case countryID = "country_id"
        case street, telephone, postcode, city, firstname, lastname
        case defaultShipping = "default_shipping"
        case defaultBilling = "default_billing"
    }
}

// MARK: - Region
struct Region: Codable {
    let region: String?
}
serg_zhd
  • 1,023
  • 14
  • 26