Let me preface that I'm a total js noob, so thank you for your help.
I am using the randomuser.me api to create a list of users. However, it only gives me the names in lowercase. So I used this script (with the help of someone else) to capitalize the names. Now I want to take that new data and create a json file. From what I've read, I need to use node.js, but have no idea how to run it properly. I've done npm install and installed this module: https://github.com/jprichardson/node-jsonfile
I'm also using firebase to put the data into a database and hosting it.
//init firebasejs
var config = {
apiKey: "AIzaSyD0F_1-ynUdzpEkQADrs6BbXUInFCkpBdM",
authDomain: "random-users.firebaseapp.com",
databaseURL: "https://random-users.firebaseio.com",
projectId: "random-users",
storageBucket: "random-users.appspot.com",
messagingSenderId: "675456550116"
};
firebase.initializeApp(config);
var database = firebase.database();
var fs = require('fs');
var jsonfile = require('jsonfile')
var file = '/tmp/data.json'
//fuunction to capitalize strings
function capitalize(text) {
return (!text || !text.length) ?
text :
(text[0].toUpperCase() + text.slice(1));
}
$.get("https://randomuser.me/api/ results=20&nat=us&inc=name,gender,picture&format=pretty",
function(data) {
if (!data || !data.results)
return;
//going through 'data' and capitalizing name and gender
data.results.forEach(function(user) {
if (user.name) {
for (var name in user.name) {
user.name[name] = capitalize(user.name[name]);
}
}
if (user.gender) {
user.gender = capitalize(user.gender);
}
});
console.log(data);
//write new data to firebase
database.ref().set(data);
//write json file?
var result = JSON.stringify(data, null, 4);
$('#randomUsers').html(result);
jsonfile.writeFile(file, data, {spaces: 2}, function(err) {
console.error(err)
})
// var fs = require("fs");
// fs.writeFile("randomUsers.json", result, function(err) {
// if (err) {
// return console.log(err);
// } else {
// console.log("The file was saved!");
// }
// });
});
Can you js wizards point me in the right direction? Thank you!