Reading an External json data - json

My script is looking at json files created on the server and retrieved using
//this is the code that gets the JSON file
jQuery.get(link,data)
Alerting out 'data' shows the json text as valid:
{"menu":[{"link":"../chapter-1/index.html","title":"Chapter 1", "dl": "chapter-1"}
I try and parse the returned 'data' variable
obj = JSON.parse(data);
So that I can loop through it and do my additional tasks.
When I try and JSON.parse(data) it fails
//The loop
for (i = 0; i < obj.menu.length; i++) {
console.log(obj.menu[i].dl);
download_packet(obj.menu[i].dl, i);
}
When I add the json manually
obj = JSON.parse('{"menu":[{"link":"../chapter-1/index.html","title":"Chapter 1", "dl": "chapter-1"}]}');
It works.
I have tried to convert to string etc.
Anyone know what I am stuffing up? - There are no error messages in the console.
This is the whole snippet:
function download_items(link) {
jQuery.get(link, function(data) {
var obj = data;
var i;
for (i = 0; i < obj.menu.length; i++) {
console.log(obj.menu[i].dl);
}
});
}

#Malcolm Mclean - put me on the right track.I converted my object to string then parsed it and it worked. A bit inefficient but its working.
Thanks Mal!
The JSON had to be parsed correctly:
jQuery.get(link, function(data) {
var obj = String(json_object);
obj = JSON.parse(json_object);
}

Related

How to 'merge' multiple objects as one json object while reading data from csv file

I want to convert some csv file into json file in nodejs.
While, some of property in the json will be array. Right now I can read a csv file row by row like this:
{"price":1,"bedrooms":"Bedrooms (All Levels): 4"},
{"price":null,"bedrooms":"Bedrooms (Above Grade): 4"},
{"price":null,"bedrooms":"Master Bedroom: 21x15"},
{"price":null,"bedrooms":"Master Bedroom Bath: Full"},
{"price":null,"bedrooms":"2nd Bedroom: 14x13"},
{"price":null,"bedrooms":"3rd Bedroom: 15x14"},
{"price":null,"bedrooms":"4th Bedroom: 15x12"}
BUT I want to get something like this:
`{"price":1,"bedrooms":["Bedrooms (All Levels): 4","Bedrooms (Above
Grade): 4","Master Bedroom: 21x15","Master Bedroom Bath: Full","2nd
Bedroom: 14x13","3rd Bedroom: 15x14","4th Bedroom: 15x12"]}`
Can someone point out some ways? I tried things like fast-csv,csv-parse,ect. But couldn't merge(push or append) the values of the same field into one field as an array.
Thanks.
the code I finished right now:
var fs = require('fs');
var csv = require('fast-csv');
var stream = fs.createReadStream("../../HouseDataDev01.csv");
csv
.fromStream(stream, {columns:true, ignoreEmpty:true, headers :
["price","bedrooms"]})
.on("data", function(data){
// console.log(data);
})
.on("end", function(){
console.log("done");
});
==========
I came up with an idea that maybe I can create an object
var NewHouse = require('../models/NewHouse.js');
//NewHouse is a schema I created before to store the csv data
var test = new NewHouse;
So that I can use the test object something like this:
.on("data", function(data){
for(i in test){
test.i.push(data[index];
}
But I found there are many other properties in test like:$__reset, $__dirty, $__setSchema
How could I write this loop?
Ok, let me explain this...
The main point in my solution is to create some thing like headtag and fieldname{}to record the stream from fs which read csv row by row. I use the headtag to validate the round of the streaming rows. For example for the first round, I need the row's value to be the key of every object in my final json file. If I set a header in fromStream() method, each round's result will conatin the header, I dont know how to 'merge' them, so I chose this 'tricky' way.
Then, as in my final json file, some of(not all of) field will be array. So when I read a second value which is not an empty string "", I should convert the field into an array.usingreadResult[fieldnames[i]]=new Array( readResult[fieldnames[i]]);.
here is the code:
//create a fime stream
var stream = fs.createReadStream(csvfilepath);
//as the file will be read row by row, headtag is pointer to row.
//e.g: headtag = 5, means the stream is reading the 5th row of the csv file
var headtag=0;
//the final stream read result, will be the same format in schema.
var readResult={};
//fieldname records the headers key name.
var fieldnames={};
csv.fromStream(stream,{ignoreEmpty:true})
.on("data", function(data){
if(headtag === 0){
//I assume the first row is the headers,so should make sure the headers' names are the same in your schema
fieldnames = data;
for(var i=+0; i<data.length; i++){
readResult["data[i]"] = {};
}
}
if(headtag === 1){
//some of fields may only conatins one value, so save them as a String
for(var i=+0; i<data.length; i++){
readResult[fieldnames[i]] = data[i];
}
}
if(headtag === 2){
for(var i=+0 ; i<data.length; i++){
//for those field that may contains multiple values, convert them as an array, then push all values in it.
if(data[i]!==""){
readResult[fieldnames[i]]=new Array( readResult[fieldnames[i]]);
readResult[fieldnames[i]].push(data[i]);
}
}
}
if(headtag > 2){
for(var i=+0 ; i<data.length; i++){
if(data[i]!==""){
readResult[fieldnames[i]].push(data[i]);
}
}
}
headtag=headtag+1;
})
.on("end",function(){
readResult.images = images;
//create a time tag
var startdate = moment().format('MM/DD/YYYY');
var starttime = moment().format('H:mm:ss');
readResult.save_date=startdate;
readResult.save_time=starttime;
//save the data in mongodb
NewHouse.create(readResult, function(error, house){
if(error){
console.log(error);
}
else{
console.log("successfully create a document in your mongodb collection!");
}
});
});
On the basis of this question, I updated my code. Now you can read both csv file and images together and save them to mongodb.
for more information check here:
https://github.com/LarryZhao0616/csv_to_json_converter

Convert .txt file to JSON

I want to convert a fairly unorganized and unstructured text file to JSON format. I want to be able to use the city ID information. Is there anyway I can convert this to JSON?
UPDATE: I also found this solution after a while. Very simple way to get the JSON of any tab separated text file.
https://shancarter.github.io/mr-data-converter/
You can try to use tsv2json this tool can reads a tsv file from stdin and writes a json file to stdout.
It's distributed in source file, to compile it you need to download D compiler and then run dmd tsv2json.d.
If you have more complex task there is another tool named tsv-utils
TSV to JSON in nodejs
var file_name = 'city_list.txt';
var readline = require('readline');
var fs = require('fs');
var lineReader = readline.createInterface({
input: fs.createReadStream(file_name)
});
var isHeader = false;
var columnNames = [];
function parseLine(line) {
return line.trim().split('\t')
}
function createRowObject(values) {
var rowObject = {};
columnNames.forEach((value,index) => {
rowObject[value] = values[index];
});
return rowObject;
}
var json = {};
json[file_name] = [];
lineReader.on('line', function (line) {
if(!isHeader) {
columnNames = parseLine(line);
isHeader = true;
} else {
json[file_name].push(createRowObject(parseLine(line)));
}
});
lineReader.on('close', function () {
fs.writeFileSync(file_name + '.json', JSON.stringify(json,null,2));
});

How to create a csv file of a form / table in ionic mobile app?

I am not able to find how I can create a csv or xls file of a table where data is inserted by users. I want a button that saves data as csv/xls file-format in ionic apps.
I have one form with a save button that saves data of the form in SQLite. I can also retrieve data in a table. Now, I want this table data to be saved in my ionic app. I used ng-csv, ng-sanitizer to handle this, this worked just fine in browser but the button does not fire in the application.
How can I handle that?
First of all,
Convert the data from json object to CSV.
function ConvertToCSV(objArray) {
var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
var str = '';
for (var i = 0; i < array.length; i++) {
var line = '';
for (var index in array[i]) {
if (line != '') line += ','
line += array[i][index];
}
str += line + '\r\n';
}
return str;
}
Then just write the csv values as writeFile to externalRootDirectory.
$ionicPlatform.ready(function() {
$scope.exportCSV = function (data){
console.log(data);
var jsonObject = JSON.stringify(data);
console.log(jsonObject);
var finalCSV = ConvertToCSV(jsonObject);
console.log(finalCSV);
//alert('cordova.file.dataDirectory: ' + cordova); //I get [object Object]
// alert('cordova.file.dataDirectory: ' + cordova.file.dataDirectory); // I get file is undefined
$cordovaFile.writeFile(cordova.file.externalRootDirectory, 'data.csv', finalCSV, true).then(function(result){
alert('Success! Export created!');
}, function(err) {
console.log("ERROR");
})
}
});
And in the emulator or android device rootDirectory, which will be get by
Console.log(cordova.file.externalRootDirectory) or alert(cordova.file.externalRootDirectory)
Go to that directories and you will get your csv file, like this you can easily export the file with any kind of format.

How to create an object of specific type from JSON in Parse

I have a Cloud Code script that pulls some JSON from a service. That JSON includes an array of objects. I want to save those to Parse, but using a specific Parse class. How can I do it?
Here's my code.
Parse.Cloud.httpRequest({
url: 'http://myservicehost.com',
headers: {
'Authorization': 'XXX'
},
success: function(httpResponse) {
console.log("Success!");
var json = JSON.parse(httpResponse.text);
var recipes = json.results;
for(int i=0; i<recipes.length; i++) {
var Recipe = Parse.Object.extend("Recipe");
var recipeFromJSON = recipes[i];
// how do i save recipeFromJSON into Recipe without setting all the fields one by one?
}
}
});
I think I got it working. You need to set the className property in the JSON data object to your class name. (Found it in the source code) But I did only try this on the client side though.
for(int i=0; i<recipes.length; i++) {
var recipeFromJSON = recipes[i];
recipeFromJSON.className = "Recipe";
var recipeParseObject = Parse.Object.fromJSON(recipeFromJSON);
// do stuff with recipeParseObject
}
Example from this page https://parse.com/docs/js/guide
var GameScore = Parse.Object.extend("GameScore");
var gameScore = new GameScore();
gameScore.save({
score: 1337,
playerName: "Sean Plott",
cheatMode: false
}, {
success: function(gameScore) {
// The object was saved successfully.
},
error: function(gameScore, error) {
// The save failed.
// error is a Parse.Error with an error code and message.
}
});
IHMO this question is not a duplicate of How to use Parse.Object fromJSON? [duplicate]
In this question the JSON has not been generated by the Parse.Object.toJSON function itself, but comes from another service.
const object = new Parse.Object('MyClass')
const asJson = object.toJSON();
// asJson.className = 'MyClass';
Parse.Object.fromJSON(asJson);
// Without L3 this results into:
// Error: Cannot create an object without a className
// It makes no sense (to me) why the Parse.Object.toJSON is not reversible

JqGrid doesn't display JSON data

I have written code in JSP and used JSON object for displaying data in JqGrid.
I know my JSON object has data (I've log it's content) but it's not displaying in JqGrid. When I copied the JSON data into JSON string, it is getting loaded and successfully displaying in Jqgrid.
This is my JSP code:
JSONObject responcedata = new JSONObject();
responcedata.put("total",totalrow);
responcedata.put("page",cpage);
responcedata.put("records",rcount);
Report obj = new Report();
responcedata = obj.ReportGrid(responcedatal);
System.out.println(responcedata);
This is the content of my JSON object:
{"total":"21″,"rows":[{"cell":["HS","H","10","5","G","9288"],"id":"1″},{"cell":["",null,null,null,"G","2099"],"id":"2″},{"cell":["HS","F","3","53","G","86578"],"id":"3″},{"cell":["HS","F","7","26","G","8268"],"id":"4″},{"cell":["HS","F","8","54","G","221"],"id":"5″},{"cell":["HS","F","5","77","G","1020"],"id":"6″},{"cell":["HS","H","14","14","G","73334"],"id":"7″},{"cell":["HS","C","21","1","G&B","1512"],"id":"8″},{"cell":["HS","F","2","105","G","4960"],"id":"9″},{"cell":["HS","F","4","21","G","86889"],"id":"10″}],"records":11}
I used this JSON string and JqGrid is displaying data. When I use JSON object, the variable responcedata (in the code above) it only shows loading but data is never shown.
function callMe() {
$.ajax({
type : "POST",
url : "action name",
data : {}
}).done(function(data) {
// dat has list there is 5 column (id,fname,lname,username,password)
var len = data.length;
var t="";
for (var i=0; i<len; ++i) {
var id = data[i].id;
var fname = data[i].fname;
var lname = data[i].lname;
var username = data[i].username;
var password = data[i].password;
t+="<tr><td>"+id+"</td><td>"+fname+"</td><td>"+lname+"</td><td>"+username+"</td><td>"+password+"</td></tr>"
}
print(t,data);
});
}
function print(t, data) {
document.getElementById("welcometext").innerHTML = t;
}
in html:
<table id="welcometext" border="1">
</table>
I have included another Jsp in the Jsp file which contains json data.
I removed that include line. Now it is working fine.