var email = req.body.emailId;
connection.query('SELECT name FROM fun WHERE email = "' + email +'" ',function(err, result,fields) {
console.log(result);
});
The console displays "result" in the below format:
[{name:'sunny'}]
Can someone tell me how to display"sunny"?
Sunny should be accessible with the following code
alert(result[0].name); //sunny!
As you can see, the JS object displayed is an array with one element, which is an object with one key (name).
So you simply index the array to the first value result[0] and then fetch that element's name value result[0].name.
Related
I'm trying to get certain data from a json link:
bittrex.com/api/v1.1/public/getticker?market=BTC-DRS
in my node IRC bot using:
https://www.npmjs.org/package/node.bittrex.api
Part of the code:
var url = ('https://bittrex.com/api/v1.1/public/getticker?market=BTC-DRS');
bittrex.options({
'apikey' : settings.ticker.apikey,
'apisecret' : settings.ticker.secretkey,
'stream' : false,
'verbose' : false,
'cleartext' : true,
});
case 'ticker':
var user = from.toLowerCase();
bittrex.sendCustomRequest(url, function(ticker, err) {
if(err) {
winston.error('Error in !ticker command.', err);
client.say(channel, settings.messages.error.expand({name: from}));
return;
}
winston.info('Fetched Price From BitTrex', ticker);
client.say(channel, settings.messages.ticker.expand({name: user, price: ticker}));
});
break;
It works but outputs in IRC
[1:21am] <nrpatten> !ticker
[1:21am] <DRSTipbot> nrpatten The current DRS price at BitTrex {"success":true,"message":"","result":{"Bid":0.00000155,"Ask":0.00000164,"Last":0.00000155}}
I have used a couple of things to get it to show only "Last" from the reply but i keep getting errors.
Or get certain data from https://bittrex.com/api/v1.1/public/getmarketsummaries
Like any info i want from:
{"MarketName":"BTC-DRS","High":0.00000161,"Low":0.00000063,"Volume":280917.11022708,"Last":0.00000155,"BaseVolume":0.33696054,"TimeStamp":"2014-10-04T15:14:19.66","Bid":0.00000155,"Ask":0.00000164,"OpenBuyOrders":33,"OpenSellOrders":138,"PrevDay":0.00000090,"Created":"2014-06-18T04:35:38.437"}
Thanks for any help
Assuming you've parsed the JSON (e.g. via JSON.parse(str);), you just use whatever property name you want to get at. For example:
var info = JSON.parse('{"MarketName":"BTC-DRS","High":0.00000161,"Low":0.00000063,"Volume":280917.11022708,"Last":0.00000155,"BaseVolume":0.33696054,"TimeStamp":"2014-10-04T15:14:19.66","Bid":0.00000155,"Ask":0.00000164,"OpenBuyOrders":33,"OpenSellOrders":138,"PrevDay":0.00000090,"Created":"2014-06-18T04:35:38.437"}');
console.log(info.Bid);
Also, on an unrelated matter, typically callback parameters follow the error-first format (e.g. (err, result) instead of (result, err)) in order to be consistent with node core and most other modules on npm.
I want to separate and put data into array. Here's my AJAX code if it succeeded.
success : function(data){
alert (data);
$('#status').append("<p>Tax Declaration: "+data[0]
+ "<br/>Barangay: "+data[1]+"</p>"
);
},
sample out put is 08-09-0001-00047,BILAY. I want them to be separated.
If you have a string that is comma separated and you want to create an array of the items in the list, use split() like this:
var values = data.split(",");
$('#status').append(
"<p>Tax Declaration: "+values[0] + "<br/>Barangay: "+values[1]+"</p>"
);
As #Andy Jones mentioned, use .split() like so:
success : function(data){
data = data.split(/\s*,\s*/g);
$('#status').append("<p>Tax Declaration: "+data[0]
+ "<br/>Barangay: "+data[1]+"</p>"
);
},
I have the following object structure and for each record I need to display the attribute name and its value. for the following example, I need to display "Name " = xxx. The attribute name can be different for each json response. These are field names of a table so I cann't use hard coded names.
How do I read the attribute value?
I tried var propname = DataInObj.DataSet[0].Record[0].properties[1] but it didn't work. Pls help
object
REcord
+attributes
-0
Name xxx
Amount 100
+attributes
-1
Name yyy
Amount 200
See this other post: Iterate over an object in Google Apps script
Code goes like this:
var dict = { "foo": "a", "bar": "b" };
function showProperties(){
var keys = [];
for(var k in dict) keys.push(k+':'+dict[k]);
Logger.log("total " + keys.length + "\n" + keys.join('\n'));
}
I've a function that gets 2 values name and value and now I'd like to turn them into a JSON, so for example lets say ive this function
function addin(name,val)
and then i'll call it
addin("age","28") -> this will return -> {age: 28}
addin("name","Roni") -> {name: Roni}
I've tried many things to find out how to make it done, Im getting many data so I tried this
var full_js = { };
_.forEach(data, function(val,name) {
full_js.name = val;
console.log(JSON.stringify(full_js));
});
again it can be any value and any name both of them are Random Strings.
but its not working, I get it as {"name": "Roni"} and {"name": "55"}.
thanks for the help.
use a different notation:
function addin(name, val) {
var full_js = {};
full_js[name] = val;
return JSON.stringify(full_js);
}
Ex:
at the moment you are always assigning the value to the name key, instead of the dynamic name key, keep in mind that the key will always be the string representation of the variable used.
for example, addin({}, 'rony'), will return {"[object Object]":"test"}
I'd like to format my query results as a single JSON object containing an array object for each record. Need help writing the script though - the JSON.stringify function is building an array of objects (My JSON is inside out!).
I can always write a function to build the JSON manually but I have a feeling there's already a function to do what I'm looking for. I just can't find it.
The JSON string I want to get:
{["id":1,"info":"Ipsum 0"], ["id":2,"info":"Ipsum 1"],
["id":3,"info":"Ipsum 2"], ["id":4,"info":"Ipsum 3"] (and so on) }
Actual Results
[{"id":1,"info":"Ipsum 0"},{"id":2,"info":"Ipsum 1"},
{"id":3,"info":"Ipsum 2"},{"id":4,"info":"Ipsum 3"},
{"id":5,"info":"Ipsum 4"},{"id":6,"info":"Ipsum 5"},
{"id":7,"info":"Ipsum 6"},{"id":8,"info":"Ipsum 7"},
{"id":9,"info":"Ipsum 8"},{"id":10,"info":"Ipsum 9"}]
My code so far (based on this example)
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database(':memory:');
db.serialize(function() {
db.run("CREATE TABLE lorem (info TEXT)");
var stmt = db.prepare("INSERT INTO lorem VALUES (?)");
for (var i = 0; i < 10; i++) {
stmt.run("Ipsum " + i);
}
stmt.finalize();
var sql = "SELECT rowid AS id, info FROM lorem";
// Print the records as JSON
db.all(sql, function(err, rows) {
console.log(JSON.stringify(rows));
});
});
db.close();
Based on what I know of JSON I was expecting the whole recordset to be enclosed with curly brackets, and each record to be enclosed with a square bracket. However I'm seeing the opposite.
Nope, you have it backward. Database results will be modeled as an array of objects - 1 array represents the results of the entire query, and each object in that array represents a single result record. In JSON, Arrays use square brackets, objects use curly braces (Same as actual JavaScript code).