computer
computer

Reputation: 113

How to convert JSON object to JavaScript object

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

Upvotes: 3

Views: 12236

Answers (3)

KpTheConstructor
KpTheConstructor

Reputation: 3291

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.

Upvotes: 0

Pabs123
Pabs123

Reputation: 3435

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;
});

Upvotes: 1

Paul
Paul

Reputation: 141829

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 );
} );

Upvotes: 3

Related Questions