1

I am trying to get a very simple Stock API to work on JSFiddle.net using Quandl API: https://www.quandl.com/blog/api-for-stock-data

If I use the current ".csv" format as below, I am returned a CSV file. If I change the format to ".json" in my API, how do I recover the data so that I can use it on a website for example?

I believe I need to use a getJSON command, but I am confused as to how it works. Can someone help me out?

HTML:

<input type="text" id="symbol">
<button id="getPrice">Get Price!</button>
<div id="result">Stock Market Ticker</div>

JavaScript:

function getPrice() {
  var symbol = $("#symbol").val();

  var baseurl = "https://www.quandl.com/api/v3/datasets/WIKI/";
  var stock = symbol+".csv";
  var endurl = "column_index=4&rows=1&api_key='8mii36sd1q46uLgSTkLm";
  var url = baseurl+ stock + "?" + endurl;

  $("#result ").html("<a href = '" + url + "' >Hyperlink</a>");

}

$("#getPrice ").click(getPrice);

My OUTPUT using stock ticker KORS (.CSV file) is: Data Close 1/5/2016 40.72

gfullam
  • 11,531
  • 5
  • 50
  • 64
NJDS
  • 11
  • 2

1 Answers1

2

I've recently answered to "How do I use a Quandl API?" with the following snippet, which you should adapt for your JSON:

var baseurl = "https://www.quandl.com/api/v3/datasets/WIKI/";
var endurl = "column_index=4&rows=1&api_key='8mii36sd1q46uLgSTkLm";
var quandlcode = "KORS"; // if is it's your choice?
var url = baseurl + quandlcode + ".json?" + endurl; // dont forget the "?"
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onload = function() {
    var data = JSON.parse(this.responseText).dataset.data;
    // {}.dataset.data is the data matrix in Quandl
    // for most datasets as far as I know ...
    // then process your own way
}
xhr.send();
Community
  • 1
  • 1
allez l'OM
  • 547
  • 4
  • 13