3

I am trying to convert JSON object data from http://api.fixer.io/latest?base=USD which contains to JavaScript object `

{
    "base": "USD",
    "date": "2016-12-14",
    "rates": {
        "AUD": 1.3319,
        "BGN": 1.8375,
        "BRL": 3.311,
        "CAD": 1.3116,
        "CHF": 1.0097,
        "CNY": 6.9052,
        "CZK": 25.388,
        "DKK": 6.986,
        "GBP": 0.78883,
        "HKD": 7.7566,
        "HRK": 7.0843,
        "HUF": 295.84,
        "IDR": 13288,
        "ILS": 3.8097,
        "INR": 67.479,
        "JPY": 114.98,
        "KRW": 1166,
        "MXN": 20.262,
        "MYR": 4.4441,
        "NOK": 8.4764,
        "NZD": 1.3849,
        "PHP": 49.716,
        "PLN": 4.1716,
        "RON": 4.2421,
        "RUB": 61.197,
        "SEK": 9.1651,
        "SGD": 1.424,
        "THB": 35.59,
        "TRY": 3.4879,
        "ZAR": 13.67,
        "EUR": 0.9395
    }
}

What i have tried so far in console

var text = $.getJSON('http://api.fixer.io/latest?base=GBP')
var obj = JSON.parse(text);

which gives me an error.

var text = $.getJSON('http://api.fixer.io/latest?base=GBP')
var obj = JSON.stringify(text, 0, 2)

which turn everything to string which is not what i wanted.

I am trying to achieve(after manage to convert them into object)

obj.rates.AUD

Which will return the value 1.3319 which is from the JSON object data. Thanks

3
  • 3
    "which gives me an error." What's the error? Commented Dec 14, 2016 at 19:18
  • I ran into an http/https error at first, you may want to drop the protocol. Commented Dec 14, 2016 at 19:20
  • Please read the documentation: api.jquery.com/jquery.getjson . Commented Dec 14, 2016 at 19:55

3 Answers 3

3

jQuery's getJSON parses the JSON for you, but the request is asynchronous, so it doesn't return the response text directly; instead it returns a jqXHR object.

This should work for you:

$.getJSON('http://api.fixer.io/latest?base=GBP').then( function ( obj ) {
  console.log( obj );
} );
Sign up to request clarification or add additional context in comments.

Comments

1

If you just look at the response of that request, you can see that the JSON comes inside of the responseText property. Just do:

var obj = JSON.parse(text.responseText);

Additionally, getJson is an asynchronous call, so you need to do this in the callback otherwise the response may not exist yet:

var obj;
var text = $.getJSON('http://api.fixer.io/latest?base=GBP', function() {
  obj = JSON.parse(text.responseText);
});

Edit: As commenter pointed out, you can also just get the JSON straight from the responseJSON property:

var obj;
var text = $.getJSON('http://api.fixer.io/latest?base=GBP', function(){
   obj = text.responseJSON;
});

3 Comments

In Chrome it appears to be already parsed in response.responseJSON
You can definitely get it from responseJSON as well!
The JSON response is also passed as an argument to the callback function as per Paulpro's answer.
0

Don't use JSON.parse() or JSON.stringify() . As demonstrated in jquery docs $.getJSON(url , callback) takes a callback . Also $.getJSON() uses $.parseJSON() behind the scenes so an object will be passed into the callback by default .

Your Problem..

var text = $.getJSON('http://api.fixer.io/latest?base=GBP') <-- returns nothing

The Solution .. add a callback function

var text = $.getJSON('http://api.fixer.io/latest?base=GBP' , function(response){

console.log(response.rates.AUD);

})

docs : http://api.jquery.com/jquery.getjson/

Hope this helps.

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.