Can anyone please help me to return the JSON string value from the get call and pass that value in mail body

This is my request. I want to return the value i.e myJson and pass myJson value inside text. Could any one please help me to resolve this.
Now I am getting mail. But Body is blank. It is not returning JSON Value

var $http = require(‘request’);
var myAccountID = ‘33333’;
var myQueryKey = ‘teere’;
var nodemailer = require(‘nodemailer’);
var request = require(‘request’);
var assert = require(‘assert’);
var Sender = ‘textt@xxx.com’;
var Recipients = '‘textt@yyy.com’;

let transporter = nodemailer.createTransport({
host: “smtp.gmail.com”,
port: 465,
auth: {
user: ‘xxx@gmail.com’,
pass: ‘xxx’
}
});
var message = {
from: Sender,
to: Recipients,
subject: ‘Test message from New Relic Synthetic monitor’,
text: ‘test’,
}
var options = {
//Define endpoint URI
uri: ‘https://insights-api.newrelic.com/v1/accounts/’+myAccountID+’/query?nrql=SELECT%20average(duration)%20as%20’Duration’%2C%20%20percentage(count(*)%2C%20WHERE%20result%3D’SUCCESS’)%20AS%20’Uptime’%2C%20%20apdex((duration%2F1000)%2C%20t%3A7)%2Cpercentage(count(timestamp)%2CWHERE%20(duration%2F1000)%20%3C%3D%207%20AND%20result%20%3D%20’SUCCESS’)%20AS%20’%25%20Satisfied’%2C%20%20percentage(count(timestamp)%2CWHERE%20(duration%2F1000)%20%3E%207%20AND%20(duration%2F1000)%20%3C%2028%20AND%20result%20%3D%20’SUCCESS’)AS%20’%25%20Tolerating’%2C%20%20(percentage(count(timestamp)%2C%20WHERE%20(duration%2F1000)%20%3E%3D%2028)%20%2B%20percentage(count(timestamp)%2C%20WHERE%20result%20%21%3D%20’SUCCESS’))%20AS%20’%25%20Frustrated’%20FROM%20SyntheticCheck%20%20%20%20%20FACET%20monitorName%20since%207%20days%20ago%20limit%2020%20WHERE%20monitorId%20%3D%20’d2cdf4c0-71d6-4aa6-ba37-e920e0685b52’’,
//Define query key and expected data type.
headers: {
‘X-Query-Key’: myQueryKey,
‘Accept’: ‘application/json’
}
};

// //Make GET request, passing in options and callback.
$http.get(options,function(err, response, body){
var data=JSON.parse(body);
var myJson=JSON.stringify(data);
const getAverageResult=data.facets[0].results[0];
console.log(“getAverageResult Is------”+getAverageResult);
return myJson
});

Are you following the code samples?

Where are you sending email in this code?

You appear to be forming the email with “test” as the body.

Yes I am following the code sample. But not sending the value as text. I am passing the json response. Find the code below. I want to return myjson and same I passed in text. But body of the mail I received is blank.

var $http = require(‘request’);
var myAccountID = ‘33333’;
var myQueryKey = ‘teere’;
var nodemailer = require(‘nodemailer’);
var request = require(‘request’);
var assert = require(‘assert’);
var Sender = ‘textt@xxx.com’;
var Recipients = '‘textt@yyy.com’;
var myJson;

let transporter = nodemailer.createTransport({
host: “smtp.gmail.com”,
port: 465,
auth: {
user: ‘xxx@gmail.com’,
pass: ‘xxx’
}
});
var message = {
from: Sender,
to: Recipients,
subject: ‘Test message from New Relic Synthetic monitor’,
text: myJson,
}
var options = {
//Define endpoint URI
uri: ‘https://insights-api.newrelic.com/v1/accounts/’+myAccountID+’/query?nrql=SELECT%20average(duration)%20as%20’Duration’%2C%20%20percentage(count(*)%2C%20WHERE%20result%3D’SUCCESS’)%20AS%20’Uptime’%2C%20%20apdex((duration%2F1000)%2C%20t%3A7)%2Cpercentage(count(timestamp)%2CWHERE%20(duration%2F1000)%20%3C%3D%207%20AND%20result%20%3D%20’SUCCESS’)%20AS%20’%25%20Satisfied’%2C%20%20percentage(count(timestamp)%2CWHERE%20(duration%2F1000)%20%3E%207%20AND%20(duration%2F1000)%20%3C%2028%20AND%20result%20%3D%20’SUCCESS’)AS%20’%25%20Tolerating’%2C%20%20(percentage(count(timestamp)%2C%20WHERE%20(duration%2F1000)%20%3E%3D%2028)%20%2B%20percentage(count(timestamp)%2C%20WHERE%20result%20%21%3D%20’SUCCESS’))%20AS%20’%25%20Frustrated’%20FROM%20SyntheticCheck%20%20%20%20%20FACET%20monitorName%20since%207%20days%20ago%20limit%2020%20WHERE%20monitorId%20%3D%20’d2cdf4c0-71d6-4aa6-ba37-e920e0685b52’’,
//Define query key and expected data type.
headers: {
‘X-Query-Key’: myQueryKey,
‘Accept’: ‘application/json’
}
};

// //Make GET request, passing in options and callback.
$http.get(options,function(err, response, body){
var data=JSON.parse(body);
myJson=JSON.stringify(data);
const getAverageResult=data.facets[0].results[0];
return myJson
});

transporter.sendMail(message, function callback (err, response, body){
assert.ok(!err, "Error sending email: "+err)
})

This is bit urgent. Can anyone please help me to find out ,what I missed here so that json is not getting returned.

On a scale of 0 to 10 where 10 is lives are at stake and 0 is “my boss wants me to do this thing” where does this land?

Not to throw another issue into this, but this method of getting insights data is discouraged with the new method being graphql.

https://docs.newrelic.com/docs/apis/nerdgraph/examples/nerdgraph-nrql-tutorial/

It’s 0.
Couldn’t make out much from the nrql link.

Did you see that you should use that endpoint rather than insights-api.newrelic.com?

Is this the endpoint you are talking about “https://api.newrelic.com/graphiql
I replace from “http://insights-api.newrelic.com/” to https://api.newrelic.com/graphiql" but it is not working.

Could you please give one example.

The link I posted is the tutorial. It has the example. Can you see it?

https://docs.newrelic.com/docs/apis/nerdgraph/examples/nerdgraph-nrql-tutorial/

Read the text and follow the tutorial. I think you will find you cannot simply change the endpoint.