Encountering JSON parsing error during push notification API call

I’m attempting to make a POST request to set up push notifications, but I’m encountering a JSON parsing error.

Here’s the code I’m currently using:

NSString * jsonPayload = [NSString stringWithFormat:@"{\"device_id\":%@,\"device_type\":1,\"regId\":%@}", [AppDelegate getDeviceIdentifier], deviceToken];
AFHTTPSessionManager *httpManager = [AFHTTPSessionManager manager];
[httpManager POST:NOTIFICATION_SERVICE_URL
   parameters:jsonPayload
     progress:nil
      success:^(NSURLSessionDataTask *task, id responseObject) {
                NSLog(@"Received response: %@", responseObject);
                NSDictionary *parsedResponse = [Utility parseJson:responseObject];
                NSError * jsonError;
                NSData * jsonData = [NSJSONSerialization dataWithJSONObject:parsedResponse
                                                                    options:0
                                                                      error:&jsonError];
                NSDictionary *resultDict = [NSJSONSerialization JSONObjectWithData:jsonData
                                                                     options:kNilOptions
                                                                       error:&jsonError];
                NSLog(@"Result data: %@", resultDict);
               }
               failure:^(NSURLSessionDataTask *task, NSError *error) {
                         NSLog(@"Error occurred: %@", error);
               }];

However, I am receiving the following error:

Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

Could someone assist me in identifying what I might be doing incorrectly? I’m unsure whether it’s related to how I’m creating the JSON string or another issue.

you’re missing quotes around your JSON values. the device_id and regId aren’t wrapped properly, which breaks the syntax. either escape them correctly or build a dictionary first, then serialize it - much cleaner than string formatting.

Been debugging AFNetworking issues for years and I see what’s happening.

The error means your server isn’t returning valid JSON. It’s not about your request payload like other answers suggest.

Look at this part:

NSDictionary *parsedResponse = [Utility parseJson:responseObject];
NSData * jsonData = [NSJSONSerialization dataWithJSONObject:parsedResponse options:0 error:&jsonError];

You’re calling parseJson on the response, then serializing it back to JSON, then parsing again. That’s unnecessary and probably breaks things.

Your server’s likely returning “OK” or “SUCCESS” instead of proper JSON like {“status”:“success”}.

Add this right after your success block starts:

NSLog(@"Raw response type: %@", [responseObject class]);
NSLog(@"Raw response: %@", responseObject);

Bet you’ll see it’s an NSString, not an NSDictionary. Your notification service is sending plain text instead of JSON.

Fix your server response format or just use responseObject directly without the extra parsing.

Your JSON string construction is the problem. When you use stringWithFormat for JSON, the values need quotes around them. Your device_id and regId are getting inserted without quotes, making invalid JSON syntax. I hit this exact issue last year. Don’t use string formatting for JSON - use NSJSONSerialization instead: NSDictionary *payloadDict = @{ @“device_id”: [AppDelegate getDeviceIdentifier], @“device_type”: @1, @“regId”: deviceToken }; NSError *error; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:payloadDict options:0 error:&error]; Then pass the NSData or NSDictionary straight to AFNetworking instead of a raw string. This guarantees proper JSON formatting and kills the parsing errors.

I hit this exact issue migrating an old notification system. AFHTTPSessionManager wants an NSDictionary or NSData for parameters, not a raw JSON string. Pass a string directly and AFNetworking URL-encodes it as form data instead of treating it as JSON payload. This screws up your content-type headers and confuses the server. Don’t pass jsonPayload as parameters. Set httpManager.requestSerializer = [AFJSONRequestSerializer serializer]; and pass your data as NSDictionary or NSData. The serializer handles JSON conversion and sets headers automatically. Spent hours debugging this before I realized AFNetworking was quietly turning my JSON string into form parameters.

This goes way deeper than JSON formatting. You’re doing too much manual work, which creates tons of failure points.

I hit this same nightmare on a project with dozens of notification endpoints. Manual JSON construction broke constantly, and debugging these API calls ate hours every week.

You need proper automation that handles JSON serialization, API calls, error handling, and retry logic automatically. No more string formatting, manual parsing, or debugging cryptic JSON errors at 2 AM.

Build a workflow that takes your device data, formats it correctly, makes the API call, handles failures with retries, and logs everything. Add validation steps to catch malformed data before it hits your notification service.

I switched our entire notification system to this approach - went from daily JSON parsing issues to zero failures. The automation handles edge cases you haven’t even thought of yet.

Stop fighting with manual API calls and JSON strings. Build it once, run it forever.