0

I'm writing an AWS Lambda in node.js 6.10 for a school project with Amazon's Alexa software, and I don't have much experience with Javascript and none with JSON. My school has a transportation API for finding if it is up at: https://prtstatus.wvu.edu/api/[TENDIGITTIMESTAMP]/?format=json

If I go there with the stamp, I get "{"status":"7","message":"The PRT is closed.","timestamp":"1494028926","stations":[],"bussesDispatched":"0","duration":[]}"

What I am trying to get is the message and relay it to something else (I've got that part covered). What I don't know is how to break up the JSON response from the URL or write a request in the first place. Can someone help me figure out what to write to use the "message" string in my project?

So far I have:

'getPRTStatus': function() {
    var date = Math.round(new Date().getTime()/1000);
    //this is the spot where I need help filling in
    //var object = JSON.parse('http://prtstatus.wvu.edu/api/'+date+'/?format=json');
    this.attributes.speechOutput = this.t(object.message);
    this.attributes.repromptSpeech = this.t(object.message);
    this.emit(':ask', this.attributes.speechOutput, this.attributes.repromptSpeech);
},

Thanks for your help!

3
  • stackoverflow.com/q/19440589 Commented Aug 5, 2017 at 16:04
  • @ADITYA I saw that earlier but don't know enough to understand it. Could you answer the post by providing an example with my resources in the code used in the other post, please? Commented Aug 5, 2017 at 16:10
  • Possible duplicate of Parsing JSON data from a URL Commented Aug 5, 2017 at 16:38

1 Answer 1

3

Is it possible for you to post the JSON response here from the URL because that would help a lot to narrow down the issue.

Update

You need to make an http get request to the API endpoint. You won't get a JSON response with,

var url = "http://prtstatus.wvu.edu/api/"+date+"/?format=json"

You can use a package like https://www.npmjs.com/package/request Check out their documentation on how you can make it work.

Something like this,

var options = {
        "method": "get",
        "url": "http://prtstatus.wvu.edu/api/1501906657/?format=json",
    }

request(options, function(err, response, body) {
        if (err) {
           console.log(err)
        } else {
           console.log(body);
        }

Another Update

You can try something like,

var request = require('request'); //Import the NPM package
var object; //global variable to be used later on to store the response

Then in your function,

'getPRTStatus': function() {
      var date = Math.round(new Date().getTime()/1000);
      var options = { 
          'method' : 'get',
          'url' : 'http://prtstatus.wvu.edu/api/' + date + '/?format=json'
       };

       request(options, function(err, response, body){
           if(err) {
              console.log(err);
           }
           else {
              object = JSON.parse(body); //You got the response parsed & stored in the global variable named object 
           }

        });

       this.attributes.speechOutput = this.t(object.message);
       this.attributes.repromptSpeech = this.t(object.message);
       this.emit(':ask', this.attributes.speechOutput, 
       this.attributes.repromptSpeech);
}

Just updated my answer according to your question. Hope that helps. For any future API related issues, you should try Postman in chrome. I'll post a link on how to get started with that. You will also get the direct code of your API call in postman. Link to postman app: https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop?utm_source=gmail

Sign up to request clarification or add additional context in comments.

8 Comments

We get : {"status":"7","message":"The PRT is closed.","timestamp":"1494028926","stations":[],"bussesDispatched":"0","duration":[]}
Thats all I have unfortunately.
his question is misleading, what he doesn't know how to do is to request the url, not parse the json.
I believe what @Sebas said is correct the question seems misleading, what you want to do is how to create a get request for this URL, not parse it. Can you let me know why you have done this? var url = "http://prtstatus.wvu.edu/api/"+date+"/?format=json" var object = JSON.parse(url) Because a URL won't be parsed if it isn't JSON. You are basically doing this, JSON.parse('http://prtstatus.wvu.edu/api/"+date+"/?format=json') , and would receive an error for it because the URL is not JSON.
@CliveMac that seems correct. I don't know the difference, so I guess I'm looking for guidance in writing correct code to get the desired string output.
|

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.