1

How can i solve this error JSON.parse: unexpected character at line 1 column 2 of the JSON data

I am trying to display specific JSON content from a ticker link, but unfortunately couldn't get it to work, as the error keep popping.

HTML

  <input id="currency1" type="text">
  <span>Currency1</span>
  <input id="currency2" type="text">
  <span>Currency2</span>
  <div>
  <button type="button" onclick="refreshPrice()">
  Refresh Price
  </button>
  <span id="lastPrice"></span>
  </div>

JSON Response from the Ticker Link

{"ticker":{"high":"16985100","low":"16730900","last":"16879000"}}

JAVASCRIPT

var lastPrice;
function refreshPrice() { 
$lastPrice = $('#lastPrice'); 
$lastPrice.html("");
$.get("https://example.com") //Ticker link
.then(function (data) {
  lastPrice = JSON.parse(data).ticker.last - 100000;
  lastPrice.html(lastPrice);
});
}

refreshPrice();
$('#currency2').keyup(function() {
currency2Val = parseFloat($(this).val());
if (currency2Val) {
 currency1Val = currency2Val * lastPrice;
  $('#currency1').val(parseInt(currency1Val));
}
else {
  $('#currency1').val("");
}
});

$('#currency1').keyup(function() {
currency1Val = parseInt($(this).val());
if (currency1Val) {
currency2Val = currency1Val / lastPrice;
  $('#currency2').val(currency2Val.toFixed(8));
}
else {
  $('#currency2').val("");
}
});

Any help would be appreciated, thanks

3
  • 1
    your response is probably HTML rather than JSON ... or it's already been parsed, and you're trying to parse an object! ... try lastPrice = data.ticker.last - 100000; Commented Mar 4, 2017 at 10:38
  • 1
    also lastPrice.html(lastPrice); doesn't make sense if lastPrice is a Number (which it surely will be) - I think you meant $lastPrice.html(lastPrice); Commented Mar 4, 2017 at 10:40
  • That's correct, and you are right regarding the $lastPrice.html(lastPrice), that solved the error message. Thanks Commented Mar 4, 2017 at 10:56

3 Answers 3

2

Try to remove JSON.parse,

var lastPrice;
function refreshPrice() { 
$lastPrice = $('#lastPrice'); 
$lastPrice.html("");
$.get("https://example.com") //Ticker link
.then(function (data) {
  debugger;
  lastPrice = data.ticker.last - 100000;
  lastPrice.html(lastPrice);
});
}

Put a debugger as well, to see what is your response.

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

Comments

1

Assuming the ticker responds with the correct Content-Type header, jQuery will already have parsed the response for you. So remove the JSON.parse:

$.get("https://example.com") //Ticker link
.then(function (data) {
  lastPrice = data.ticker.last - 100000;
  $lastPrice.html(lastPrice);
});

(I've also included a fix there for the issue Jaromanda X pointed out, using lastPrice rather than $lastPrice.)

From the documentation:

dataType (default: Intelligent Guess (xml, json, script, or html))

Type: String

The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string).

Comments

0

Response is already in JSON format, you shouldn't be passing it through JSON.parse, as JSON.parse expects input to be a stringify JSON

Try this :

   lastPrice = data.ticker.last - 100000;

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.