I am trying to extract certain values from JSON data with NodeJS - javascript

Here is the situation:
I have a Rails app that scrapes a website and outputs valid JSON to a restful API endpoint.
So far I can get the JSON with my Node.js script, but I cannot store the values I need to local variables.
Here is the json:
[{"cohort":"1507","teacher":"Josh M."}]
I would like to store "1507" in a variable as well as "Josh M."
My current script is:
var http = require('http');
var url = 'http://localhost:8080/api/v1/classroom_bs';
http.get(url, function(res){
var body = '';
res.on('data', function(chunk){
body += chunk;
});
res.on('end', function(){
var responseB = JSON.parse(body);
var responseBStr = JSON.stringify(body);
var jsonB = JSON.parse(responseBStr);
console.log(responseB);
console.log(responseBStr);
console.log(jsonB);
});
}).on('error', function(e){
console.log("Error: ", e);
});
Any help would be greatly appreciated!
I have tried some functions I found on SO, but for some reason all my console.log(Object.keys) return numbers like "1", "2", "3", etc..
The reason I need to store these as variables is that I am going to output those values to an LCD screen using the johnny5 library via an Arduino.

The rails app that you are using to send to node is an array of length 1, which is an object. To access that object you have to say which array element you are accessing. In this case array[0]. It appears your rails scraper is populating an array.
To access the variables you mentioned you would simply
//Access that 1507
var variable1 = payloadFromRails[0].cohort;
//Access that teach, Josh M.
var variable2 = payloadFromRails[0].teacher;

Try selecting object at index 0 of parsed json string
var response = '[{"cohort":"1507","teacher":"Josh M."}]';
var responseB = JSON.parse(response);
var keys = Object.keys(responseB[0]);
var cohort = responseB[0][keys[0]];
var teacher = responseB[0][keys[1]];
console.log(cohort, teacher)

Related

Discord.JS Bot request npm getting json elements

So I've been trying to call this one website for a json of event data and I can't quite figure out how to get the first chunk of data in the json. This is the website https://api.tftech.de/Event/ I'm only trying to pull this data and then make each one of the values into a variable. [{"name": "Spooky Event","start": "2020-07-27T02:35:00-05:00","end": "2020-07-27T03:35:00-05:00","priority": "HIGH","color": "ff9800","intervalInMinutes": 7440}] Can anyone help?
You can use axios to make requests,
Here is the code block with the results put into variables
var axios = require("axios")
axios.get("https://api.tftech.de/Event/").then(data => {
var name = data.data[0].name;
var start = data.data[0].start;
var end = data.data[0].end;
var priority = data.data[0].priority;
var color = data.data[0].color;
var intervalInMinutes = data.data[0].intervalInMinutes;
})

Struggling with the wunderground api and node

So I had been stuck on this exercise in Treehouse some time ago and just moved on. I came back to it now that I understand things better and I'm still fighting with the wunderground api. I've read through the json data and documentation, updated a few things from when the class was first recorded (and the API updated since then), and still am getting errors I can't field. I've got three js files- app.js, weather.js, and api.json (which is just my api key so not shared here.)
After my corrections, I'm still getting the error "TypeError: Cannot read property 'temp_f' of undefined" which doesn't make sense as I keep reading over the JSON to check that it's pointing to the right place.
Can anyone put an end to my misery trying to fix this?
App.js:
const weather = require('./weather');
//Join multiple values passed as arguments and replace all spaces with underscores
const query = process.argv.slice(2).join("_").replace(' ', '_');
//query: 90201
//query: Cleveland_OH
//query: London_England
weather.get(query);
Weather.js
const https = require('https');
const http = require('http');
const api = require('./api.json');
// Print out temp details
function printWeather(weather) {
const message = `Current temp in ${weather.location} is ${weather.current_observation.temp_f}F`;
console.log(message);
}
// Print out error message
function get(query) {
const request = http.get(`http://api.wunderground.com/api/${api.key}/conditions/q/${query}.json`, response => {
let body = "";
// Read the data
response.on('data', chunk => {
body += chunk;
});
response.on('end', () => {
//Parse data
const weather = JSON.parse(body);
//Print the data
printWeather(weather);
});
});
}
module.exports.get = get;
//TODO: Handle any errors

Using Node.js to find the value of Bitcoin on a webpage at real time

I'm trying to make a .js file that will constantly have the price of bitcoin updated (every five minutes or so). I've tried tons of different ways to web scrape but they always output with either null or nothing. Here is my latest code, any ideas?
var express = require('express');
var path = require('path');
var request = require('request');
var cheerio = require('cheerio');
var fs = require('fs');
var app = express();
var url = 'https://blockchain.info/charts/';
var port = 9945;
function BTC() {
request(url, function (err, res, body) {
var $ = cheerio.load(body);
var a = $(".market-price");
var b = a.text();
console.log(b);
})
setInterval(BTC, 300000)
}
BTC();
app.listen(port);
console.log('server is running on '+port);
It successfully says what port it's running on, that's not the problem. This example (when outputting) just makes a line break every time the function happens.
UPDATE:
I changed the new code I got from Wartoshika and it stopped working, but im not sure why. Here it is:
function BTCPrice() {
request('https://blockchain.info/de/ticker', (error, response, body) => {
const data = JSON.parse(body);
var value = (parseInt(data.USD.buy, 10) + parseInt(data.USD.sell, 10)) / 2;
return value;
});
};
console.log(BTCPrice());
If I have it console.log directly from inside the function it works, but when I have it console.log the output of the function it outputs undefined. Any ideas?
I would rather use a JSON api to get the current bitcoin value instead of an HTML parser. With the JSON api you get a strait forward result set that is parsable by your browser.
Checkout Exchange Rates API
Url will look like https://blockchain.info/de/ticker
Working script:
const request = require('request');
function BTC() {
// send a request to blockchain
request('https://blockchain.info/de/ticker', (error, response, body) => {
// parse the json answer and get the current bitcoin value
const data = JSON.parse(body);
value = (parseInt(data.THB.buy, 10) + parseInt(data.THB.sell, 10)) / 2;
console.log(value);
});
}
BTC();
Using the value as callback:
const request = require('request');
function BTC() {
return new Promise((resolve) => {
// send a request to blockchain
request('https://blockchain.info/de/ticker', (error, response, body) => {
// parse the json answer and get the current bitcoin value
const data = JSON.parse(body);
value = (parseInt(data.THB.buy, 10) + parseInt(data.THB.sell, 10)) / 2;
resolve(value);
});
});
}
BTC().then(val => console.log(val));
As the other answer stated, you should really use an API. You should also think about what type of price you want to request. If you just want a sort of index price that aggregates prices from multiple exchanges, use something like the CoinGecko API. Also if you need real-time data you need a websocket-based API, not a REST API.
If you need prices for a particular exchange, for example you're building a trading bot for one or more exchanges, you;ll need to communicate with each exchange's websoceket API directly. For that I would recommend something like the Coygo API, a node.js package that connects you directly to each exchange's real-time data feeds. You want something that doesn't add a middleman since that would add latency to your data.

How to access MongoDB rather than JSON file

I have created a little application that implemented a JSON file as the data store. I'm trying to learn about MEAN, so I'm trying to convert it to a NODE.js app.
I have created a mongoDB importing my JSON file using mongoimport. The db is there and connected. When I put http://localhost:3000/api/characters into the browser it returns JSON for all the characters.
I have built a connection string and required it into my controller as follows...
// FROM node app characters.controller.js
var dbconn = require('../data/dbconnection.js');
var characterData = require('../data/characters.json');
module.exports.charactersGetAll = function(req, res) {
var db = dbconn.get();
var collection = db.collection('characters');
// Get the documents from the collection
// Need to use to Array() as its only a cursor if you don't
collection
.find()
.toArray(function(err, docs) {
console.log("Found characters!", docs);
res
.status(200)
.json(docs);
});
//console.log('db', db);
//console.log("GET the Characters");
//console.log(req.query);
res
.status(200)
.json(characterData);
};
module.exports.charactersGetOne = function(req, res) {
var charId = req.params.id;
var thisChar = characterData[charId];
console.log("GET characterId", charId);
res
.status(200)
.json(thisChar);
};
From the non-NODE version of the app, I call in the JSON data as follows in my main.js file:
//FROM TRADITIONAL HTML/JS application
// Get JSON and callback so JSON can be stored globally
$.getJSON('people.json', callback);
// Populate faces and names in HTML
function callback(data) {
/*optional stuff to do after success */
var $charNames = $('.char-names');
var $faceImage = $('.faces');
$.each(data, function(index, val) {
console.log("success");
/* iterate through array or object */
/* .eq() method constructs new object from one element from set */
$charNames.eq(index).text(val.name);
$faceImage.eq(index).attr('src', val.image);
//Push all JSON to array
allTheCharacters.push(val);
allTheCharactersComp.push(val);
});
}
What I want to ask is – Is there a simple way I can access the mongoDB in the non-NODE main.js file instead of using the $.getJSON method and how would I add/adapt this for the node characters.controller.js
Hopefully, I am making sense. Apologies in advance for any misunderstanding.

convert json array to var values in node.js express.js

i am trying go convert my json array in to simple (numbered)values.
getting them from database(.find), the problem is they are not converting with json.parse. am i doing something wrong?
Voltage.find({},function (err, data) {
var jsontext = data;
var parsedData = JSON.parse(jsontext);
res.json(parsedData);
console.log(parsedData);
});
ths is the console.log for the session, i was hoping for just: 333, 333, 333 etc.
[{"_id":"56f3c19a0298308405d60464","temp":333,"__v":0},{"_id":"56f3c1ee7ec57884068dcb2c","temp":333,"__v":0},{"_id":"56f3c4467ec57884068dcb2d","temp":333,"__v":0},{"_id":"56f3d80191a3c68c138bf04d","temp":337,"__v":0},{"_id":"56f3da3f06cefa781763fb21","temp":337,"__v":0}]
it is the temp values i am trying to get out to send to my front end only. i am using mongooose, express.js and node.js with a mongodb also.
thanks for looking.
One thing you could do is deselect them in the query:
Voltage.find().select('-_id -__v').exec(function (err, data) {
var jsontext = data;
var parsedData = JSON.parse(jsontext);
res.json(parsedData);
console.log(parsedData);
});
Read up on the select method here.

Categories