-2

var realmStatus = "http://api.dol.gov/V1/Statistics/OES/OE_AREATYPE/?KEY=1ce7650d-b131-4fb7-91b3-b7761efc8cd4";

    //"http://api.dol.gov/V1/WHPS/?KEY=1ce7650d-b131-4fb7-91b3-b7761efc8cd4";
    //source: http://stackoverflow.com/questions/17811827/get-a-json-via-http-request-in-nodejs

var http = require("http");

var options = {
        host: 'api.dol.gov',
        path: realmStatus,
        type: 'GET',
        dataType: 'json',
        headers: {'accept' : 'application/json'}
    };



console.log("Start");
var x = http.request(options,function(res){
    console.log("Connected");
    res.on('data',function(data){
        console.log(data.toString()+"\n");
    });
});
x.end();

I have a bunch of json files that i grab from a site api tables which outputs different type of json data, and just want to know is there a way to store these files into variable or in an array.

MY partner said this , but don't know what he means and how to do it. I got filtering the data for each tables.

"So what we know now is that each requst will give us one table. What you need to do, is store those variables that are given to us from the JSON file by storing them in a variable or an array. When dealing with JSON, you an access parameters by using the reference to the JSON file, then using the . operator to access each variable inside of the JSON file. If you can just print those values stored in the JSON file, then this is pretty much a success with the user story. It means we can access those values and make cross comparisons with what the user has and the data given to us from the table."

Chris
  • 11
  • 1
  • 5
  • 1
    have you written any code for this yet? Also you should add a language tag. – Jonathan Portorreal Mar 26 '17 at 19:16
  • yea i posted it , so the files have the same code but different api data link. so it output different data. i want to take these files and turn it into a variable or an array – Chris Mar 26 '17 at 19:56

1 Answers1

1

Hey so from my understanding you want three different api results in an array. I suggest you try to create a general function that calls an api and concats results this way you can just pass that function all the api endpoints you need and get back all the results from an array. better ways to achieve the end result.

var realmStatus = "http://api.dol.gov/V1/Statistics/OES/OE_AREATYPE/?KEY=1ce7650d-b131-4fb7-91b3-b7761efc8cd4";

var http = require("http");

var options = {
  host: 'api.dol.gov',
  path: realmStatus,
  type: 'GET',
  dataType: 'json',
  headers: {
    'accept': 'application/json'
  }
};


function getJson(endpoint) {

  options.path = endpoint;

  return new Promise((resolve, reject) => {
    var x = http.request(options, function(res) {
      res.on('data', function(data) {
        resolve(JSON.parse(data));
      })

    });

    x.end();
  });
}


file1 = getJson(realmStatus);
file2 = getJson(realmStatus);
file3 = getJson(realmStatus);

Promise.all([file1, file3, file3]).then(values => {
  console.log(values);
});

So the function takes an array of api endpoints (here i just used realmStatus multiple times) and then makes a request and uses a promise to store the json response. Using Promises.all you can then aggregate the promise's and the end result is an array of the json from different api calls.

However, if you actually made three api calls from different node scripts and you have three files of json you can use fs or any other helpful node library for reading files and just read all the files into an array. I hope this gave you a clue or even an answer!

Community
  • 1
  • 1
Jonathan Portorreal
  • 2,730
  • 4
  • 21
  • 38