0

I have to send a URLRequest containing a JSON object as its http-body (which needs to be of type Data).

My JSON looks like this:

let json: [String : Any] = [
        "to" : toDeviceToken,       // String
            "notification" : [
                "title" : title,    // String
                "body" : body,      // String
                "icon" : icon       // UIImage
            ],
            "data" : {
                // Add optional data here.
            }
        ]

And I would like to convert this to type Data in order to put it into a URLRequest.

This

try JSONSerialization.data(withJSONObject: json, options: .prettyPrinted)

always fails for some reason, maybe because it is a nested JSON object? In addition, JSONSerialization.isValidJSONObject(json) returns false.

When turning the UIImage object into a String object like this UIImagePNGRepresentation(UIImage.appIcon!)?.base64EncodedString(), I get an error saying 'Invalid type in JSON write (_SwiftValue)'

How would I do that?

7
  • Possible duplicate of Convert Dictionary to JSON in Swift Commented Jan 20, 2018 at 22:47
  • try JSONSerialization.data(withJSONObject: ... Commented Jan 20, 2018 at 22:47
  • @LouFranco This does not work for me, maybe because it's a nested JSOn object? Commented Jan 20, 2018 at 22:51
  • 1
    FYI, people often use multipart/form-data requests when uploading an image, as the base64 string representation introduces 33% overhead. If it's a small image, that's inconsequential. But if it's a large image or if you're uploading many, it can start to become material. Tools like Alamofire make uploading multipart/form-data requests really easy. But if doing it manually, the code is a little more tedious. Commented Jan 20, 2018 at 23:53
  • 1
    I would also suggest using JPEG representation which will reduce considerably the data size Commented Jan 21, 2018 at 0:10

1 Answer 1

1

Your code fails because it contains data that can't be converted to JSON.

See the documentation for JSONSerialization for a list of support data types. Note that UIImage is not one of the supported types.

You will need to convert the UIImage to a string using a base64 encoding of its JPG or PNG representation.

Sign up to request clarification or add additional context in comments.

1 Comment

UIImagePNGRepresentation(UIImage.appIcon!)?.base64EncodedString() So this code should work, but it always throws a very long error 'Invalid type in JSON write (_SwiftValue)'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.