Extract data from json object in jQuery or JS

I want to use the currency data provided by https://raw.github.com/currencybot/open-exchange-rates/master/latest.json

As an initial test, I created a shortened version of this as an inline object:

var obj = [ { "disclaimer": "This data is collected from various providers and provided free of charge for informational purposes only, with no guarantee whatsoever of accuracy, validity, availability, or fitness for any purpose; use at your own risk. Other than that, have fun! More info: http://openexchangerates.org/terms/", "license": "Data collected from various providers with public-facing APIs; copyright may apply; not for resale; no warranties given. Full license info: http://openexchangerates.org/license/", "timestamp": 1339036116, "base": "USD", "rates": { "EUR": 0.795767, "GBP": 0.645895, "JPY": 79.324997, "USD": 1 } }]; 

All I want to do is somehow request / grep / filter the json object with, for example, "EUR" as criteria and return it with a variable called "rate" with the value "0.795767" as the result.

I looked at the grep and jQuery filter functions, but I canโ€™t figure out how to isolate only the bid section of the object and then get the speed I need.

+7
source share
2 answers
 var obj = [ { "disclaimer": "This data is collected from various providers and provided free of charge for informational purposes only, with no guarantee whatsoever of accuracy, validity, availability, or fitness for any purpose; use at your own risk. Other than that, have fun! More info: http://openexchangerates.org/terms/", "license": "Data collected from various providers with public-facing APIs; copyright may apply; not for resale; no warranties given. Full license info: http://openexchangerates.org/license/", "timestamp": 1339036116, "base": "USD", "rates": { "EUR": 0.795767, "GBP": 0.645895, "JPY": 79.324997, "USD": 1 } }]; obj[0].rates.EUR; // output: 0.795767 

or

 obj[0].rates['EUR']; output: //0.795767 

Demo

If you want to highlight bids in another variable and use this variable, try the following:

 var rates = obj[0].rates; 

Now,

 rates.EUR; rates.GBP; 

etc.

+18
source

You can also use the JSON.parse() Javascript function to convert a JSON string to a Javascript JSON object.

 var JSONObject = JSON.parse("{'value1' : 1, 'value2' : 2}"); console.log(JSONObject.value1); // Prints '1'.. 
+5
source

All Articles