How to set utf-8 to csvtojscon? - json

I want to extract the data from csv file, for that I am using csvtojson npm , it is working fine but if the csv file contains any foregin letters then it return "��" in my json so can anyone guide me here
var converter = require("csvtojson");
function(req, data){
var array = [];
var json = await convert().fromFile(filepath.csv);
array.push(json)
// continuation of my code here
}

try this
var converter = require("csvtojson");
var json = await convert().fromFile(filepath.csv,{ encoding: 'binary' });

Related

How to export JSON-like data in spreadsheet column to json file?

I have a Google sheet with JSON-like data in a column and would like to export this column as a JSON file. I have tried using javascript along with xlsx package to convert the sheet to json file but it adds backslashes to the column and cannot be parsed (throws syntax error) using JSON.parse() as it does not recognise it as valid json. Any help is appreciated!
let xlsx = require("xlsx")
let path = require("path")
let fs = require("fs");
const inputFilePath = path.join(__dirname, './Sample.xlsx');
let File = xlsx.readFile(inputFilePath);
let content = xlsx.utils.sheet_to_json(File.Sheets['Sheet1']);
console.log(JSON.parse(content[0]["content"])); //throws error
Here is an example that will write the data without backslash (do not use JSON.stringify in this case). The file will be in 'test' folder here, that you have to create or change in the script.
// you need to activate the Advanced Drive Service (Drive Activity API).
function test() {
var content = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange('A1').getValue();
var folders = DriveApp.getFoldersByName("test");
if (folders.hasNext()) {
var folder = folders.next();
saveData(folder, 'myJSON.json',content);
}
}
function saveData(folder, fileName, content) {
var children = folder.getFilesByName(fileName);
var file = null;
if (children.hasNext()) {
file = children.next();
file.setContent(content);
} else {
file = folder.createFile(fileName, content);
}
}
https://docs.google.com/spreadsheets/d/1PWzdlaZi2m0a1xDiqLp2eJXIvx-AyvZ16CQW362q-Nw/edit?usp=sharing
Of course, replace A1 by B2 for your file.

Google script - parse json response

This is the json response (from my multiple choice field jsfiddle) that i'm trying to parse:
{"selected":true,"disabled":false,"text":"Ctr","id":"Ctr","title":"","_resultId":"select2-selectcountry-result-je97-Ctr","element":{}},
{"selected":true,"disabled":false,"text":"Title
Part1","id":"TitlePart1","title":"","_resultId":"select2-selectcountry-result-uv7s-TitlePart1","element":{}},
{"selected":false,"disabled":false,"text":"Milan","id":"Milan","_resultId":"select2-selectcountry-result-bmba-Milan","element":{}}]
I need to get: {"id":value},{"id":value},{"id":value} ...
{id:Ctr},{"id":"TitlePart1"},{"id":"Milan} ...
To achieve this result, I'm using this code:
var response = (JSON.stringify($('#selectcountry').select2('data')) );
var json = JSON.parse(response);
var dataSet = json;
var row = [],
data;
for(var i in json){
data = dataSet[i];
row.push({'id': json[i].id})
}
sheet.getRange(6,1).setValue(row);
But in this way I get only the first id:value:
{id:Ctr}
Any help?
Thanks
var s='[{"selected":true,"disabled":false,"text":"Ctr","id":"Ctr","title":"","_resultId":"select2-selectcountry-result-je97-Ctr","element":{}},{"selected":true,"disabled":false,"text":"Title Part1","id":"TitlePart1","title":"","_resultId":"select2-selectcountry-result-uv7s-TitlePart1","element":{}},{"selected":false,"disabled":false,"text":"Milan","id":"Milan","_resultId":"select2-selectcountry-result-bmba-Milan","element":{}}]';
function findId() {
var d=JSON.parse(s);
var ids=[];
d.forEach(function(o){
ids.push(o.id);
});
Logger.log(ids);
//Add this
SpreadsheetApp.getActiveSheet().getRange(1,1,1,3).setValues([ids]);
}

Access JSON position in Node.js

I have a JSON string in this format:
[
{
"Origin":{
"FtpHost":"info",
"FtpFolder":"info",
"FtpUser":"info",
"FtpPassword":"info",
"FtpInsideFolder":"info",
"Pattern":"info"
},
"Destination":{
"FtpHost":"info",
"FtpFolder":"info",
"FtpUser":"info",
"FtpPassword":"info",
"FtpInsideFolder":"info"
},
"CustomFolderName":"Conad",
"OperationTraverseType":"RootOnly"
}
]
To pick up the JSON I wrote this in Node.js:
var fs = require('fs');
var obj = fs.readFileSync('Operations.json', 'utf8');
I'm wondering, how I can access for example : "Destination" fields?
You must parse this to JSON. because fs.readFile returns string
var fs = require('fs');
var obj = fs.readFileSync('Operations.json', 'utf8');
obj = JSON.parse(obj)
var Destination = obj[0].Destination
// or
var Destination = obj[0]["Destination"]
Edit (as said Diego)
You can also directly require json file
var obj = require('somejsonfile.json');
var Destination = obj[0]. Destination
Just need to simply parse the read data. Something like this:
var fs = require('fs');
var obj = fs.readFileSync('Operations.json', 'utf8').toString();
obj = JSON.parse(obj)
console.log(obj[0].Destination)
you can do like var myjson = JSON.parse(obj) or obj = JSON.parse(fs.readFileSync('Operations.json', 'utf8')) and then access it like obj[0]["Destination"]["FIELD"] where FIELD - represents the "Destination" object field you want

reading/writing large files with PapaParse/BabyParse

I have a large CSV file (~500mb) that I want to convert to JSON using BabyParse (the node version of PapaParse). With smaller files I can read the CSV into a string and then pass the string to parse. However, a 500mb file is to too big to be read into a string in this way.
I have a workaround that reads the CSV file as a stream line-by-line, but it's horrendously slow (see below). Can someone tell me a faster way to work with large CSV files in Papa/Baby parse?
var Baby = require('babyparse');
var fs = require('fs');
var readline = require('readline');
var stream = require('stream');
var file = '500mbbigtest.csv';
//var content = fs.readFileSync(file, { encoding: 'binary' }); DOESN'T WORK
var instream = fs.createReadStream('500mbbigtest.csv');
var outstream = new stream;
var rl = readline.createInterface(instream, outstream);
rl.on('line', function(line) {
parsed = Baby.parse(line, {fastMode: false});
rows = parsed.data;
rows = JSON.stringify(rows);
fs.appendFileSync("blahblahblah.json", rows);
});

Uncaught SyntaxError, Unexpected Identifier in for loop in jade

I am trying to render a jade with some dynamic content. I am reading from a json in jade.
My json looks like this
{ data1: 'data1',
data2: 'data2',
data3:
[ { name: 'ABC',
address: 'India'
},
{ name: 'DEF',
address: 'Australia'
}]}
I am trying to render a jade and use the data from above json
my jade looks like
var data1 = #{data1};
var data2 = #{data2};
var size = #{data3.length};
for data in #{data3}
var name = data.name;
var address = data.address;
I am able to correctly extract data in the first 3 lines mentioned above. But when I try to fetch data from within a loop, I get "Uncaught SyntaXError, Unexpected Identifier" error while debugging.
If i put a line outisde the for loop, it works fine. Ex
var name = #{data3[0].name};
is rendered properly. But i need to iterate over a loop and fetch data over there. Can somebody help.
Thanks
Updating with more information
1. I have node server running where I create a json -
var json_string = "{"data1":"data1","data2":"data2","data3":[{"name":"ABC","address":"India"},{"name":"DEF","address":"Australia"}]};";
var json_data = JSON.parse(json_string);
console.log(json_data);
res.render('sample_example', json_data);
In my sample_example.jade I have the following snippet within script
var data1 = #{data1};
var data2 = #{data2};
var size = #{data3.length};
for data in #{data3}
var name = data.name;
var address = data.address;
As stated earlier, I am able to properly extract #{data1}, #{data2}, #{data3.length} to the variables . But it breaks within the for loop. In fact, I am able to extract #{data3[0].name} from outside the for loop. But within the for looop it gives the stated error.
This is how you can do it now.
In your server-side you have to JSON.stringify the array of objects.
var json_data = JSON.parse(json_string);
// Convert back to json only the property data3
json_data.data3 = JSON.stringify(json_data.data3);
res.render('simple', json_data);
Or the better is to not parse the JSON just let it go the way it is:
// var json_data = JSON.parse(json_string);
res.render('simple', {
json_data: json_string
});
And in the Jade Template (If you followed the better method):
script(type='text/javascript').
var json_data = !{json_data};
var data1 = json_data.data1;
var data2 = json_data.data2;
var data3 = json_data.data3;
var size = data3.length;
data3.forEach(function(data) {
var name = data.name;
var address = data.address;
console.log(name, address);
});
Also you need to change the loop structure. The for..in used to iterate over objects not array of objects.
This works for me;
- var cdata = {"data1":"data1","data2":"data2","data3":[{"name":"ABC","address":"India"},{"name":"DEF","address":"Australia"}]};
each data in cdata.data3
- var name = data.name;
- var address = data.address;
p Name: #{name}
p Address: #{address}
Can you share the actual jade file contents if updating the code as shown above doesn't work. Also what version of jade and express?