Search code examples
jqueryjsonajaxgetjson

SyntaxError: Unexpected token in JSON at position x


Well I know this question is asked many times. But this one is different.

I'm using getJSON to get some data from Database. The result that is returned is a valid JSON (checked by several JSON validators), but I receive the following error:

SyntaxError: Unexpected token in JSON at position 1618

which has a special character in it which doesn't show up in the above error.

This is the screenshot of the error message:

enter image description here

the character at position 1618 is a space (code is 32), and it is inside a text value.

Here is the JSON text:

{"headers":["default","fa-ir","en-us"],"results":[{"id":14,"default":"آذربایجان شرقی","fa-ir":"آذربایجان شرقی","en-us":""},{"id":15,"default":"آذربایجان غربی","fa-ir":"آذربایجان غربی","en-us":""},{"id":16,"default":"اردبیل","fa-ir":"اردبیل","en-us":""},{"id":17,"default":"اصفهان","fa-ir":"اصفهان","en-us":""},{"id":18,"default":"البرز","fa-ir":"البرز","en-us":""},{"id":19,"default":"ایلام","fa-ir":"ایلام","en-us":""},{"id":20,"default":"بوشهر","fa-ir":"بوشهر","en-us":""},{"id":21,"default":"تهران","fa-ir":"تهران","en-us":""},{"id":22,"default":"چهارمحال و بختیاری","fa-ir":"چهارمحال و بختیاری","en-us":""},{"id":23,"default":"خراسان جنوبی","fa-ir":"خراسان جنوبی","en-us":""},{"id":24,"default":"خراسان رضوی","fa-ir":"خراسان رضوی","en-us":""},{"id":25,"default":"خراسان شمالی","fa-ir":"خراسان شمالی","en-us":""},{"id":26,"default":"خوزستان","fa-ir":"خوزستان","en-us":""},{"id":27,"default":"زنجان","fa-ir":"زنجان","en-us":""},{"id":28,"default":"سمنان","fa-ir":"سمنان","en-us":""},{"id":29,"default":"سیستان و بلوچستان","fa-ir":"سیستان و بلوچستان","en-us":""},{"id":30,"default":"فارس","fa-ir":"فارس","en-us":""},{"id":31,"default":"قزوین","fa-ir":"قزوین","en-us":""},{"id":32,"default":"قم","fa-ir":"قم","en-us":""},{"id":33,"default":"کردستان","fa-ir":"کردستان","en-us":""},{"id":34,"default":"کرمان","fa-ir":"کرمان","en-us":""},{"id":35,"default":"کرمانشاه","fa-ir":"کرمانشاه","en-us":""},{"id":36,"default":"کهگیلویه و بویراحمد","fa-ir":"کهگیلویه و بویراحمد","en-us":""},{"id":37,"default":"گلستان","fa-ir":"گلستان","en-us":""},{"id":38,"default":"گیلان","fa-ir":"گیلان","en-us":""},{"id":39,"default":"لرستان ","fa-ir":"لرستان ","en-us":""},{"id":40,"default":"مازندران","fa-ir":"مرکزی","en-us":""},{"id":41,"default":"هرمزگان","fa-ir":"هرمزگان","en-us":""},{"id":42,"default":"همدان","fa-ir":"همدان","en-us":""},{"id":43,"default":"یزد","fa-ir":"یزد","en-us":""}]}

Solution

  • Your JSON (retrieved from http://sabad123.com/ajax/update-input.aspx?t=province&id=8) has a tab ("\t") at character 1618, right here:

    ...{"id":39,"default":"لرستان \t",...
    

    This is failing to parse in Python's JSON parser as well as $.getJSON.

    I would next take a look at how you're producing this JSON. Since it doesn't seem to be valid, there may be a bug in whatever's producing it.

    At the very least, you can fix up this specific data problem by getting rid of the tab.

    EDIT

    Note that, indeed, per the JSON spec, a tab is not an allowed character in a JSON string. See https://stackoverflow.com/a/19799355/94559. I think \u0009 is the proper way to escape the tab character.