2

I tried to store the JSON data into a variable in node from JSON file which is located at https://data.sfgov.org/resource/wwmu-gmzc.json Tell me what can I do for that?

Thank you in advance!

2
  • 1
    You need a REST client library (hint: search npm), do GET call to that Url and store the result into a variable. Commented Jul 13, 2018 at 13:28
  • 1
    Post the code you used. We can help you with the code you've written, not write it for you. Commented Jul 13, 2018 at 13:30

1 Answer 1

2
  • use mongoose npm to save data to mongodb database

  • use request npm to make http request to url

Make Model :

  var mongoose =require('mongoose')  
  var Schema = mongoose.Schema;

  var movieSchema = new Schema({
         actor_1: String,
         actor_2: String,
         actor_3:  String,
         director: String,
         locations: String,
         production_company:  String,
         release_year: String,
         title:  String,
         writer: String,
  });

 var Movie = mongoose.model('Movie', movieSchema);

in your app.js

 var request = require('request');
    var data=[];
    request('https://data.sfgov.org/resource/wwmu-gmzc.json', function(error, response, body) {
        console.log('error:', error);
        console.log('statusCode:', response && response.statusCode); 
        console.log('body:', body);
        if(response && response.statusCode==200){
        var data=body;
        for(var i=0;i<data.length;i++){
             var movie=new Movie(data[i]);
             movie.save();
        }
      }

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

Comments

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.