Converting Google Visualization Query result into JSON - google-visualization

I am a beginner trying to read a Google Sheet in a Javascript app using: https://docs.google.com/spreadsheets/d/mySheetID/gviz/tq?tq=Select%20*%20where%20A%20=%20%22Nan%22&tqx=out:JSON
I can access that row in my sheet and save it as JSON giving me a file with the content headed "/O_o/
google.visualization.Query.setResponse..." This is the file I cannot further process in javascript.
I see in: converting Google Visualization Query result into javascript array
that the solution appears to be: "If you add a header named X-DataSource-Auth in your request, the Visualization API will respond in JSON format". After a day of googling I am quite unable to find where I am supposed to put such a header and what its syntax should be.
But then I'm 82 years old and this stuff gets more difficult with each passing year... Can someone point me in the right direction?

once you receive the response from the query,
you can convert the response to a google data table.
var dataTable = response.getDataTable();
and the google data table has a method to convert the data table to JSON.
var jsonData = dataTable.toJSON();
this will return a JSON string.
if you then would like to work with the JSON using JavaScript,
you can parse the string...
jsonData = JSON.parse(jsonData);
the JSON will be an object with two properties, cols and rows.
you can see an example of the JSON result here...
see this fiddle for a working example using the following code...
https://jsfiddle.net/WhiteHat/5mu9wnbz/1/
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var query = new google.visualization.Query('https://docs.google.com/spreadsheets/d/1M3wQgKg3JBF6_hzv1xWONP7HWVYoOvJ1jPbB27IUg94/gviz/tq?gid=0&headers=1');
query.send(function (response) {
if (response.isError()) {
console.log('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
return;
};
var dataTable = response.getDataTable();
var jsonData = dataTable.toJSON();
jsonData = JSON.parse(jsonData);
document.getElementById('cols').innerHTML = jsonData.cols.length;
document.getElementById('rows').innerHTML = jsonData.rows.length;
console.log(jsonData);
});
});

Related

How to extract the values from the response body in postman and store as variable

I am trying to extract the sys_id value and store it as a variable within postman. Currently I am not getting any errors using the following
var data = JSON.parse(responseBody);
pm.environment.set('sys_id', pm.response.json().sys_id);
It is saving the variable, but showing null within the value
Response Body
{
"result": {
"sys_id": "5ae690c11ba421d46557a9b7bd4bcbbf",
}}
Any help will be appreciated!
Without knowing the whole value of the response body - and based on this link, you can try this code - which I tested with another JSON data payload:
Code:
let data = pm.response.json();
pm.environment.set('sys_id', data.result.sys_id);
console.log(pm.variables.get("sys_id"));
Managed to resolve it with the following code:
var responseData = JSON.parse(responseBody);
postman.setEnvironmentVariable("sys_id", responseData.result.sys_id);

Validating JSON schema in Postman

When using Postman I validate the JSON response like so:
tv4.addSchema(globalSchema);
const valResult = tv4.validate(data, schema);
// schema is an object, which is a subschema from the larger globalSchema
which works fine, except for the error reporting. The error object I get is missing dataPath and schemaPath, making it hard for my user to find out where the actual problem is. Is there a way to get those properties? (tried validateResult and validateMultiple to no avail)
As an alternative I tried ajv, but as I am in draft-04, it gives me errors. The advice from their site
var ajv = new Ajv({schemaId: 'id'});
// If you want to use both draft-04 and draft-06/07 schemas:
// var ajv = new Ajv({schemaId: 'auto'});
ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-04.json'));
does not work because the Postman sandbox does not allow me to require that… any thoughts?
See also: https://community.getpostman.com/t/json-schema-validation-troubles/5024
Here's how I validate schema's with postman to get more detailed errors:
const schema = {
};
var jsonData = JSON.parse(responseBody);
pm.test('Checking Response Against Schema Validation', function() {
var result=tv4.validateMultiple(jsonData, schema);
console.log(result);
pm.expect(result.valid).to.be.true;
});

How to Use UrlFetch in Google App script for getting Inner fields of Facebook page feed?

I am trying to get the inner fields of facebook page feed json data through urlfetch function in Google App script but i am getting
invalid argument
error.
I saw some other resolution but non of them work in my case this is my codes
I am trying to get the atttachment inner fields eg fields=attachments{subattachments,url} the braces are the problem here so how to parse this through urlfetchApp?
var url = 'https://graph.facebook.com'
+ '/pageneme/feed'
+'?fields=name,full_picture,message,attachments{subattachments,url}&access_token=MYACCESStoken'
var response = UrlFetchApp.fetch(url, {'muteHttpExceptions': true});
var json = response.getContentText();
var jsondata = JSON.parse(json);
var data =jsondata.data
var pagename =data[0].name
var images= data[0].full_picture
var messages =data[0].message
var attachment = data[0].attachments
var media =data[0].subattachments
Logger.log(images); //check this and adjust following for loop and html
showFeed function accordingly
Logger.log(messages);
Logger.log(attachment);
Got it by myself thanks for those down voted
I had unsafe characters in my url. I had to encode the url:
var url = 'https://graph.facebook.com'
+ '/pagename/feed'
// + '?access_token=' + encodeURIComponent(getToken());
+'?fields='+
encodeURIComponent("name,full_picture,message,attachments{subattachments,url}")
+'&access_token=token'

Parse-Server Cloud Code Query Doesn't Return All Columns

I have setup Parse-Server on AWS Elastic Beanstalk by following this guide. I've then written a cloud-code function which fetches a single record from a specific class/collection. The collection contains about 20 columns. However, the object fetched as a result of the query contains only about 8 columns. I've made sure the record does have data in the columns which are missed by the query. Am I missing something here or is it some limitation in Parse? Is there any way to force Parse to fetch these columns?
Parse.Cloud.define('confirmAppointment', function(request, response) {
var staffId = request.params.staffId;
var appointmentId = request.params.appointmentId;
var appointmentRequest = Parse.Object.extend("AppointmentRequest");
appointmentRequest.id = appointmentId;
appointmentRequest.staffId = staffId;
var query = new Parse.Query(appointmentRequest);
query.first({
useMasterKey: true,
success: function(appointment) {
if (appointment) {
// these fields are not found in the fetched appointment object
// they do exist however in mongodb
var requesterUserId = appointment.get("requesterUserId");
var staffUserId = appointment.get("staffUserId");
var staffName = appointment.get("staffNameEn");
...
}
}
...
});
});
There might be some typos in your code (the construction of the query part). Try this instead:
Parse.Cloud.define('confirmAppointment', function(req, res) {
var staffId = req.params.staffId;
var appointmentId = req.params.appointmentId;
var query = new Parse.Query("AppointmentRequest");
query.equalTo('objectId', appointmentId);
query.equalTo('staffId', staffId);
query.first({
useMasterKey: true,
success: function(appointment) {
res.success(appointment.get("requesterUserId"));
},
error: function(err) {
res.error(err);
}
});
});
The issue turned out to be that when i did migration of data from Parse to my mongolab hosted MongoDB instance, I did not click 'Finalize' button in Parse migration wizard. That was intentional, as Parse was warning me that clicking Finalize would make the migration permanent and I would no longer be able to get back to the Parse managed database. On the other hand, I could see that all the data was successfuly migrated to mongolab, and technically it should have been enough to have my AWS hosted parse server work on this new database without any issue. But somehow, clicking "Finalize" button in Parse did some magic (I still dont understand what it could be) and my queries started returning the expected results.
I was able to reproduce the same issue when migrating to Heroku as well, so i was sure it had nothing to do with AWS.
Hope this would help someone.

Ext.data.jSonP Sencha API with Coldfusion

I am trying to get a JSon from my server. I am calling the API like this:
Ext.data.JsonP.request({
url: 'http://dev.mysite.com/temp.cfm',
callbackKey: 'callback',
timeout: 40000,
params: {
format: 'json'
},
success: function(result, request) {
// Get the weather data from the json object result
var weather = result; console.log('Succ');
},
failure: function(result, request) {
// Get the weather data from the json object result
var weather = result; console.log('Fail');
},
callback: function(result, request) {
// Get the weather data from the json object result
var weather = result; console.log('CallB');
}
});
I am using Coldfusion as Serverside. So, I am simply doing this:
<cfreturn '#url.callback#({\"LOGINSTATUS\":\"fail\"})'>
That returns the following string:
Ext.data.JsonP.callback1({\"LOGINSTATUS\":\"fail\"})
But my request always times out.
I couldn't figure out what was the actual problem. I just tried using cfm file instead of cfc on server side and everything started working.
If anyone could explain why this happened I'll accept that explanation as correct answer.
Thanks DmitryB and Sharondio for you time and trying to help me fix it. I really appreciate your help.