Get usable array from csvtojson
up vote
1
down vote
favorite
I've seen code examples of using csvtojson, but each one simply outputs the result using console.log(). The following code creates a variable named 'json', but I don't see how I can use this outside of the function creating it. How can I export the variable 'json' for use outside of the function that is creating it?:
const csvFilePath='<path to csv file>'
const csv=require('csvtojson')
csv().fromFile(csvFilePath,function(err,result)
if(err)
console.log("An Error Has Occured");
console.log(err);
var json = result; // I want to use this var outside of this function.
console.log(json);
);
I was really hoping that it would be as simple as writing something like:
const dataArray = csv().fromFile(csvFilePath);
But, dataArray doesn't contain my data, but instead appears to be an object with parameters about the data.
Any clues will be greatly appreciated!
javascript node.js csvtoarray
add a comment |
up vote
1
down vote
favorite
I've seen code examples of using csvtojson, but each one simply outputs the result using console.log(). The following code creates a variable named 'json', but I don't see how I can use this outside of the function creating it. How can I export the variable 'json' for use outside of the function that is creating it?:
const csvFilePath='<path to csv file>'
const csv=require('csvtojson')
csv().fromFile(csvFilePath,function(err,result)
if(err)
console.log("An Error Has Occured");
console.log(err);
var json = result; // I want to use this var outside of this function.
console.log(json);
);
I was really hoping that it would be as simple as writing something like:
const dataArray = csv().fromFile(csvFilePath);
But, dataArray doesn't contain my data, but instead appears to be an object with parameters about the data.
Any clues will be greatly appreciated!
javascript node.js csvtoarray
2
Possible duplicate of How do I return the response from an asynchronous call?
– Lux
Nov 10 at 21:05
1
Very close:const jsonArray = await csv().fromFile(csvFilePath);npmjs.com/package/csvtojson#from-csv-file-to-json-array
– Jonathan
Nov 10 at 22:31
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I've seen code examples of using csvtojson, but each one simply outputs the result using console.log(). The following code creates a variable named 'json', but I don't see how I can use this outside of the function creating it. How can I export the variable 'json' for use outside of the function that is creating it?:
const csvFilePath='<path to csv file>'
const csv=require('csvtojson')
csv().fromFile(csvFilePath,function(err,result)
if(err)
console.log("An Error Has Occured");
console.log(err);
var json = result; // I want to use this var outside of this function.
console.log(json);
);
I was really hoping that it would be as simple as writing something like:
const dataArray = csv().fromFile(csvFilePath);
But, dataArray doesn't contain my data, but instead appears to be an object with parameters about the data.
Any clues will be greatly appreciated!
javascript node.js csvtoarray
I've seen code examples of using csvtojson, but each one simply outputs the result using console.log(). The following code creates a variable named 'json', but I don't see how I can use this outside of the function creating it. How can I export the variable 'json' for use outside of the function that is creating it?:
const csvFilePath='<path to csv file>'
const csv=require('csvtojson')
csv().fromFile(csvFilePath,function(err,result)
if(err)
console.log("An Error Has Occured");
console.log(err);
var json = result; // I want to use this var outside of this function.
console.log(json);
);
I was really hoping that it would be as simple as writing something like:
const dataArray = csv().fromFile(csvFilePath);
But, dataArray doesn't contain my data, but instead appears to be an object with parameters about the data.
Any clues will be greatly appreciated!
javascript node.js csvtoarray
javascript node.js csvtoarray
asked Nov 10 at 20:57
Tony Guinta
365148
365148
2
Possible duplicate of How do I return the response from an asynchronous call?
– Lux
Nov 10 at 21:05
1
Very close:const jsonArray = await csv().fromFile(csvFilePath);npmjs.com/package/csvtojson#from-csv-file-to-json-array
– Jonathan
Nov 10 at 22:31
add a comment |
2
Possible duplicate of How do I return the response from an asynchronous call?
– Lux
Nov 10 at 21:05
1
Very close:const jsonArray = await csv().fromFile(csvFilePath);npmjs.com/package/csvtojson#from-csv-file-to-json-array
– Jonathan
Nov 10 at 22:31
2
2
Possible duplicate of How do I return the response from an asynchronous call?
– Lux
Nov 10 at 21:05
Possible duplicate of How do I return the response from an asynchronous call?
– Lux
Nov 10 at 21:05
1
1
Very close:
const jsonArray = await csv().fromFile(csvFilePath); npmjs.com/package/csvtojson#from-csv-file-to-json-array– Jonathan
Nov 10 at 22:31
Very close:
const jsonArray = await csv().fromFile(csvFilePath); npmjs.com/package/csvtojson#from-csv-file-to-json-array– Jonathan
Nov 10 at 22:31
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
The problem is that fromFile is asynchronous, and once you have called async code, you live in the callback.
The module doesn't seem to provide a sync alternative. Your options are:
- Continue your program logic inside the callback:
csv().fromFile(csvFilePath,function(err,result)
if(err)
console.log("An Error Has Occured");
console.log(err);
var json = result;
console.log(json);
// Continue your logic here
// .....
// .....
);
- Use async/await
You seem to be using an older version of csvtojson, so you might need to upgrade it to use this:
(async () =>
var jsons = await csv().fromFile(csvFilePath);
console.log(jsons);
)()
.catch((err) =>
console.log(err);
);
Basically this wraps your code inside an async function. Inside it, you can use the return value of fromFile if you use the await keyword.
- Use a different module that supports sync loading or even do the parsing yourself.
add a comment |
up vote
0
down vote
csvJSON function with functional map
var csvJSON = function(csv)
var lines = csv.split("n");
var result = ;
var headers = lines[0].split(",");
lines.map(function(line, indexLine)
if (indexLine < 1) return // Jump header line
var obj = ;
var currentline = line.split(",");
headers.map(function(header, indexHeader)
obj[header] = currentline[indexHeader];
)
result.push(obj);
)
result.pop(); // remove the las`enter code here`t item because undefined values
return result; // JavaScript object
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
The problem is that fromFile is asynchronous, and once you have called async code, you live in the callback.
The module doesn't seem to provide a sync alternative. Your options are:
- Continue your program logic inside the callback:
csv().fromFile(csvFilePath,function(err,result)
if(err)
console.log("An Error Has Occured");
console.log(err);
var json = result;
console.log(json);
// Continue your logic here
// .....
// .....
);
- Use async/await
You seem to be using an older version of csvtojson, so you might need to upgrade it to use this:
(async () =>
var jsons = await csv().fromFile(csvFilePath);
console.log(jsons);
)()
.catch((err) =>
console.log(err);
);
Basically this wraps your code inside an async function. Inside it, you can use the return value of fromFile if you use the await keyword.
- Use a different module that supports sync loading or even do the parsing yourself.
add a comment |
up vote
1
down vote
accepted
The problem is that fromFile is asynchronous, and once you have called async code, you live in the callback.
The module doesn't seem to provide a sync alternative. Your options are:
- Continue your program logic inside the callback:
csv().fromFile(csvFilePath,function(err,result)
if(err)
console.log("An Error Has Occured");
console.log(err);
var json = result;
console.log(json);
// Continue your logic here
// .....
// .....
);
- Use async/await
You seem to be using an older version of csvtojson, so you might need to upgrade it to use this:
(async () =>
var jsons = await csv().fromFile(csvFilePath);
console.log(jsons);
)()
.catch((err) =>
console.log(err);
);
Basically this wraps your code inside an async function. Inside it, you can use the return value of fromFile if you use the await keyword.
- Use a different module that supports sync loading or even do the parsing yourself.
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
The problem is that fromFile is asynchronous, and once you have called async code, you live in the callback.
The module doesn't seem to provide a sync alternative. Your options are:
- Continue your program logic inside the callback:
csv().fromFile(csvFilePath,function(err,result)
if(err)
console.log("An Error Has Occured");
console.log(err);
var json = result;
console.log(json);
// Continue your logic here
// .....
// .....
);
- Use async/await
You seem to be using an older version of csvtojson, so you might need to upgrade it to use this:
(async () =>
var jsons = await csv().fromFile(csvFilePath);
console.log(jsons);
)()
.catch((err) =>
console.log(err);
);
Basically this wraps your code inside an async function. Inside it, you can use the return value of fromFile if you use the await keyword.
- Use a different module that supports sync loading or even do the parsing yourself.
The problem is that fromFile is asynchronous, and once you have called async code, you live in the callback.
The module doesn't seem to provide a sync alternative. Your options are:
- Continue your program logic inside the callback:
csv().fromFile(csvFilePath,function(err,result)
if(err)
console.log("An Error Has Occured");
console.log(err);
var json = result;
console.log(json);
// Continue your logic here
// .....
// .....
);
- Use async/await
You seem to be using an older version of csvtojson, so you might need to upgrade it to use this:
(async () =>
var jsons = await csv().fromFile(csvFilePath);
console.log(jsons);
)()
.catch((err) =>
console.log(err);
);
Basically this wraps your code inside an async function. Inside it, you can use the return value of fromFile if you use the await keyword.
- Use a different module that supports sync loading or even do the parsing yourself.
edited Nov 11 at 9:55
answered Nov 11 at 9:43
mihai
22.8k73968
22.8k73968
add a comment |
add a comment |
up vote
0
down vote
csvJSON function with functional map
var csvJSON = function(csv)
var lines = csv.split("n");
var result = ;
var headers = lines[0].split(",");
lines.map(function(line, indexLine)
if (indexLine < 1) return // Jump header line
var obj = ;
var currentline = line.split(",");
headers.map(function(header, indexHeader)
obj[header] = currentline[indexHeader];
)
result.push(obj);
)
result.pop(); // remove the las`enter code here`t item because undefined values
return result; // JavaScript object
add a comment |
up vote
0
down vote
csvJSON function with functional map
var csvJSON = function(csv)
var lines = csv.split("n");
var result = ;
var headers = lines[0].split(",");
lines.map(function(line, indexLine)
if (indexLine < 1) return // Jump header line
var obj = ;
var currentline = line.split(",");
headers.map(function(header, indexHeader)
obj[header] = currentline[indexHeader];
)
result.push(obj);
)
result.pop(); // remove the las`enter code here`t item because undefined values
return result; // JavaScript object
add a comment |
up vote
0
down vote
up vote
0
down vote
csvJSON function with functional map
var csvJSON = function(csv)
var lines = csv.split("n");
var result = ;
var headers = lines[0].split(",");
lines.map(function(line, indexLine)
if (indexLine < 1) return // Jump header line
var obj = ;
var currentline = line.split(",");
headers.map(function(header, indexHeader)
obj[header] = currentline[indexHeader];
)
result.push(obj);
)
result.pop(); // remove the las`enter code here`t item because undefined values
return result; // JavaScript object
csvJSON function with functional map
var csvJSON = function(csv)
var lines = csv.split("n");
var result = ;
var headers = lines[0].split(",");
lines.map(function(line, indexLine)
if (indexLine < 1) return // Jump header line
var obj = ;
var currentline = line.split(",");
headers.map(function(header, indexHeader)
obj[header] = currentline[indexHeader];
)
result.push(obj);
)
result.pop(); // remove the las`enter code here`t item because undefined values
return result; // JavaScript object
answered Nov 10 at 22:57
Mehar
1
1
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243335%2fget-usable-array-from-csvtojson%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Possible duplicate of How do I return the response from an asynchronous call?
– Lux
Nov 10 at 21:05
1
Very close:
const jsonArray = await csv().fromFile(csvFilePath);npmjs.com/package/csvtojson#from-csv-file-to-json-array– Jonathan
Nov 10 at 22:31