'Error while parsing JSON "Unescaped control character around character 981"

Error Domain=NSCocoaErrorDomain Code=3840 "Unescaped control character
around character 981." UserInfo={NSDebugDescription=Unescaped control
character around character 981.}

I am getting the above error in response to a request.

Below are the lines of code:

Alamofire.request(.POST, urlStr, parameters: parameter, encoding: .JSON, headers: nil).validate().responseJSON {
    response in switch response.result {

        case .Success(let JSON):
            completionHandler(JSON as! NSDictionary)

        case.Failure(let Error):

            print(Error)
    }
}

It gives a JSON response in Postman.

The response which is I am getting in Postman:

{
  "orderdetails": {
    "status_code": "200",
    "status_message": "Order details",
    "billingandshipping": {
      "billing": {
        "firstname": "first",
        "lastname": "last",
        "email": "[email protected]",
        "address": "dasdesfrew",
        "city": "Rajkot",
        "area": "University Road",
        "pincode": "360003",
        "phone": "1234567890",
        "mobileno": "1234567891"
      },
      "shipping": {
        "firstname": "first",
        "lastname": "last",
        "email": "[email protected]",
        "address": "dasdesfrew",
        "city": "dasdesfrew",
        "area": "dcdc",
        "pincode": "360003",
        "phone": "1234567890",
        "mobileno": "1234567891"
      }
    },
    "orders": [
      {
        "order_id": "77",
        "order_date": "09-08-2016 13:05:29",
        "delivery_date": "10-08-2016",
        "order_items": [
          {
            "Sr": "1",
            "product_name": "Lemon",
            "gujtitle": "લીંબુ ",
            "product_code": "000057",
            "product_price": "108.00",
            "product_qty": "2",
            "unit": "1 kg.",
            "product_total": "216"
          }
        ],
        "final_total": "216.00",
        "shipping_cost": "0.00",
        "order_total": "216.00",
        "discount_type": "null",
        "discount_amount": "null",
        "coupon_name": "null",
        "comment": "gdhdj\nfghd.g\nghj\n\n\n\n\n\n\n\n\n\n.."
      }
    ]
  }
}


Solution 1:[1]

As per you were told, there is a problem related to "\n".

So I suggest you can add "" which will work for you, like below:

"\n"=> "\\n"

Because this is a special character called a backspace character.

Solution 2:[2]

NSLog the NSData that you received and have a look what you find around byte 981. The thing with unescaped control characters is that they are invisible, so you can't see them in an NSString, but you'll see them in the NSData.

If your data has length 981 bytes or very close then there's a chance that your code processed incomplete JSON data which will almost always fail; that's something you need to fix. If there is a control character between some items (say between two array elements) then this might be a bug in the server code.

Solution 3:[3]

I spent some time to figure out what 49546 was. If your issue is Unescaped control character around character 49546, replace \t with \\\t.

Solution 4:[4]

To be sure (as people make foul copy/paste...), I build my object safe:

...

private final func fillWith(
     id: Int,
     name: String?
) {

    self.id = id
    self.productName = name?.replacingOccurrences(of: "\t", with: "")
    self.productName = self.productName?.replacingOccurrences(of: "\n", with: "")

So no problem when sending up.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter Mortensen
Solution 2 gnasher729
Solution 3 Peter Mortensen
Solution 4 Peter Mortensen