i'm using Tabulator to Mange some tables and i've see how to download the file in JSON format and others but i wonder if there's a function included to save data in JSON object instead so I can upload it directly to the server
i've checked this page http://tabulator.info/docs/4.1/download and i got the idea to use "Custom File Formatter" then intercept the download bolb but i keep getting an error referring to
setFileContents(names.join(", "), "text/plain");
Uncaught ReferenceError: setFileContents is not defined
function code:
document.querySelector("#uploadJSON").onclick = function(){
var fileFormatter = function(columns, data, options){
var names = [];
data.forEach(function(row){
names.push(row.name);
});
setFileContents(names.join(", "), "text/plain");
}
table.download(fileFormatter, "test.json");
}
You can use the getData function to get an array of row data objects:
var data = table.getData();
And then JSON encode it with the stringify function:
var jsonData = JSON.stringify(data);
It is then down to you to choose how to upload it to the server, in-fact several ajax libraries will even do the JSON encoding for you so you would just need to pass them the results from the getData function
Related
I need to get gtfs data and show raw data on screen. I tried to use gtfs-stream module, but not sure, how to get data from it.
var gtfs_source = 'https://www.bkk.hu/gtfs/budapest_gtfs.zip'
request.get(gtfs_source).pipe(gtfs.enhanced()).on('data', (entity) => {
console.log(entity)
})
gtfs-utils has a rich set of functions, but it is unable to get data from a compressed file.
Please help me to write textbox value / data into existing JSON file. I have tried with code below but unable with this. I am able to write in "textarea" but not in existing JSON file please let me know how to refer existing JSON file here and write data on that file.
var app = angular.module('myApp', []).controller('myCtrl', function ($scope) {
$scope.person = {};
$scope.getJSON = function () {
console.log("Creating a JSON");
$scope.jsonString = angular.toJson($scope.person, true);
};
});
My JSON file existing in dir: DataBase/DataBase.json
Most browsers don't allow javascript to access file system, most probably you would not be able to do this. I could suggest you to send your json data to a backend and rewrite file there.
Keep your json file inside mongodb or sqlite,then you can do all curd operation in the Json data
I have json file with given data (total.json)
var data = {"trololo":{"info":"61511","path".... }}
I need to get object "info" and then print data "61511" in alert window
I include my json like
var FILE = 'total'
var data_file_names = {};
data_file_names[FILE] = 'total.json';
And then i use it like
var data_trololo = data_file_names[FILE];
Plese, help me print object "info". Maybe there is another way to solve this problem
You need to make an ajax call to the json file. Then you can access the array like the below example.
Note : Your json wasn't properly formatted.
var data = {
"trololo":{
"info": ["61511","path"]
}
};
console.log(data.trololo.info[0]); //this one will print 61511
Usually one can make an ajax call to read the file on the server.
But if you are ok with using HTML5 features then go through the link find out how to read the file on the browser itself. Though File API being part of HTML5 spec is stable across browsers.
http://www.html5rocks.com/en/tutorials/file/dndfiles/
I'm creating a D3 bubble chart, using json data. I'm using sinatra for my app, and my json data is available at localhost:4567/data.json
I tried using
var myData = [];
$.get('data.json', function(data) {
myData = data;
console.log(myData);
.......
and I get the correct values in the javascript console, but the bubble chart does not render. (The rest of the code works if I copy and paste the data from 'data.json' and set it to a var, but it does not work if I use the $get method).
Do you have any ideas on how I could access this json data from localhost:4567?
Much appreciated,
Tim
I think what is probably going on is that jquery isn't automatically parsing the data as a JSON object due to missing MIME headers in the response from your server. Try using getJSON instead.
you can simply use
d3.json('data.json', function(data) {
myData = data;
console.log(myData);
.......
to read the json file
I need to call a web service from javascript that returns a JSON array in the following format:
["hello","there","I","am","an","array"]
Unfortunately, the javascript library I'm using (Sencha Touch) to load this data into a widget doesn't accept that format as data input. It will work with something like this however:
[["hello"],["there"],["I"],["am"],["an"],["array"]]
So there are two questions here-- how can I call that web service in javascript and then manipulate the returned array into the format I need? I've been looking at jQuery's getJson() method, not sure if that's the way to go or if there are better ways.
The URL that delivers the JSON array is this right here. Thanks for your help.
Here's a jsFiddle showing both parts of what you asked about. I'm using jQuery for my AJAX call (faked in the jsFiddle) and I'm using Underscore.js for the manipulation:
http://jsfiddle.net/JohnMunsch/E7YTQ/
// Part 1: Get the raw data. Unfortunately, within a jsFiddle I can't go get it from a different domain. So I'm
// simulating the call instead.
var rawDataPromise = $.ajax({
url : "/echo/json/",
data : fakeData,
type : "POST"
});
// var rawDataPromise = $.ajax("http://fastmotorcycleservice.cloudapp.net/FastMotorCycleListService.svc/list/Bruno");
rawDataPromise.done(
function (results) {
// Part 2: Manipulate the data into the form we need.
var manipulatedResults = _.map(results, function (item) { return [ item ]; });
console.log(manipulatedResults);
}
);
// You could also pull that together into a single call $.ajax(...).done(function (results) { ... });
once you have the data in one of your local variables you can manipulate it as you want:
var data = the_function_you_use_to_get_data();
var formatted_array = new Array();
for(i in data){
var d = new Array();
d[0] = i;
formatted_array.push(d);
}
i hope it answered your question