0

I am using a web server that returns JSON but jquery fails to parse that json and shows following error

Uncaught SyntaxError: Unexpected token ' in JSON at position 1

The data returned by api seems to be fine. I copied the response in my js file and tried to parse it with $.parseJSON but I get the same error. Here is the code snippet containing returned json and a call to parseJSON

var jso = "['ADCP1_SNR_CH1','ADCP1_SNR_CH2','ADCP1_SNR_CH3','ADCP1_RADVEL_CH0']";
        var dt = $.parseJSON(jso);

My question is, what is wrong with the above json array? why do I bump into this error?

2
  • 1
    json.org You can run the JSON through a service like JSONlint.com too. Basically, string literals have to be within double quotes. Commented Aug 1, 2017 at 16:49
  • Also duplicate of SyntaxError: Unexpected token ' in JSON at position 1. 9 times out of 10, searching for the error message gets you what you want. Someone with your rep should know we expect people to do their research... Commented Aug 1, 2017 at 16:52

2 Answers 2

2

The problem is that single quotes are not valid in JSON. Swap the single and double quotes, like so:

var jso = '["ADCP1_SNR_CH1","ADCP1_SNR_CH2","ADCP1_SNR_CH3","ADCP1_RADVEL_CH0"]';
    var dt = $.parseJSON(jso);

Alternatively, you can escape the quotes like this:

var jso = "[\"ADCP1_SNR_CH1\",\"ADCP1_SNR_CH2\",\"ADCP1_SNR_CH3\",\"ADCP1_RADVEL_CH0\"]";
    var dt = $.parseJSON(jso);
Sign up to request clarification or add additional context in comments.

Comments

2

JSON does not support single quotes ('). It must use double quotes:

var jso = '["ADCP1_SNR_CH1","ADCP1_SNR_CH2","ADCP1_SNR_CH3","ADCP1_RADVEL_CH0"]';
var dt = $.parseJSON(jso);
console.log(dt);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Comments

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.