How to handle semicolons when generating a CSV file? - node.js

I am using the NPM module json-csv to generate a CSV file from an array of objects. However, some fields might contain a semicolon (;), and apparently the CSV gets split at the semicolon, despite the fact that this field is quoted. Can anyone make any suggestions as to how to fix this issue?
The code I use for the options is the following:
var options = {
fields: [
{
name : 'paragraphId',
label : 'ParagraphID'
},
{
name : 'paragraph',
label : 'Paragraph',
quoted : true
}
]
};

According to CSV specification, you can have delimiters in values, as long as you surround these values with double quotes. From CSV specification:
Fields with embedded commas must be delimited with double-quote characters.
And:
Fields may always be delimited with double quotes.
The delimiters will always be discarded.
Option to trigger this behavior on when exporting data using json-csv library is quoted: true in the options for a given field - I see that you've already included it, so you're good.
Also - it's worth to note that this library uses comma (,) as delimiter by default, not semicolon (;). To use different delimiter, alter your options properly:
var options = {
fields: [
{
name: 'paragraphId',
label: 'ParagraphID'
},
{
name: 'paragraph',
label: 'Paragraph',
quoted: true
}],
fieldSeparator: ';' // the important part!
};

Related

Array with two objects and string how to remove Quotes

I had to .concat two objects and a string, the string didn't have surrounding quotes and now it does, I have tried .replace in every different way, please, is there a way to remove the quotes surrounding a string inside array with other objects
var keywrdrtrn = {city_name:'${city_name}'}
var obj =v[0].concat(v[1]).concat(keywrdrtrn);
}
obj [
{
city_name: 'Cincinnati',
},
{
city_name: 'Mumbai',
},
"{city_name:'',state: ''}"
]
I have removed other index/objects for space but the format is same. I just cannot figure out how to concat the setup without quotes being added or figuring out how to remove quotes after concat
I tried .replace and I tried to convert string to object prior to concat

Preserve the leading zeroes of data in excel without changing the original value

I am trying to preserve the leading zeroes of data in excel. I came across several solutions where they're suggesting to append something to the value so that the excel treats it as a string.
For example :
data + String.fromCharCode(8203)
By doing this, "​" this is printing in the excel, which is not what I want. Also, all such solutions are changing the original value. So I'm trying to preserve the leading zeroes in excel and at the same time keep the data from changing. So, in the process I've observed that the number (0123) in my node.js code, when I'm appending it into a CSV file, it is converting into (83). Similarly, (012) in node.js as (10) in CSV and hence excel also. Why is this happening, and to which format is it converting to? Please help me with this, thanks in advance.
Here's the node.js code :
const csvWriter = createCsvWriter({
path : 'outputTaskA.csv',
header : [
{id : 'empID', title : 'empID'}
]
});
const records = [
{
empID : 001234
},
{
empID : 234
},
{
empID : 345
}
];
csvWriter.writeRecords(records).then(() => {
console.log("created the file succesfully");
});
This is the output.csv file :
empID
668
234
345
To preserve leading zeroes in Excel you can use:
val1,="00123",val3
This is Excel specific, not sure how other parsers will treat this.
Just quoting a value does not make it a string in csv (since csv has no notion of data types), quotes are there to preserve embedded separators inside fields.
It is converting from octadecimal to decimal values. Node treats all 0-leading numbers as a prefix for octadecimal representation. i.e.:
Welcome to Node.js v15.12.0.
Type ".help" for more information.
> 00123
83
is it not possible to double quote the value of empID so that node treats it as string? e.g.
const records = [
{
empID : "001234"
},
{
empID : "234"
},
{
empID : "345"
}
];

nodejs skipping single quote from json key in output

I see a very weird problem when json when used in nodejs, it is skipping single quote from revision key . I want to pass this json as input to node request module and since single quote is missing from 'revision' key so it is not taking as valid json input. Could someone help how to retain it so that I can use it. I have tried multiple attempts but not able to get it correct.
What did I try ?
console.log(jsondata)
jsondata = {
'splits': {
'os-name': 'ubuntu',
'platform-version': 'os',
'traffic-percent': 100,
'revision': 'master'
}
}
Expected :-
{ splits:
{ 'os-name': 'ubuntu',
'platform-version': 'os',
'traffic-percent': 100,
'revision': 'master'
}
}
But in actual output single quote is missing from revision key :-
{ splits:
{ 'os-name': 'ubuntu',
'platform-version': 'os',
'traffic-percent': 100,
revision: 'master'
}
}
Run 2 :- Tried below code this also produce same thing.
data = JSON.stringify(jsondata)
result = JSON.parse(data)
console.log(result)
Run 3:- Used another way to achieve it
jsondata = {}
temp = {}
splits = []
temp['revision'] = 'master',
temp['os-name'] = 'ubuntu'
temp['platform-version'] = 'os'
temp['traffic-percent'] = 100
splits.push(temp)
jsondata['splits'] = splits
console.log(jsondata)
Run 4: tries replacing single quotes to double quotes
Run 5 : Change the order of revision line
This is what is supposed to happen. The quotes are kept only if the object key it’s not a valid JavaScript identifier. In your example, the 'splits' & 'revision' don't have a dash in their name, so they are the only ones with the quotes removed.
You shouldn't receive any error using this object - if you do, update this post mentioning the scenario and the error.
You should note that JSON and JavaScript are not the same things.
JSON is a format where all keys and values are surrounded by double quotes ("key" and "value"). A JSON string is produced by JSON.stringify, and is required by JSON.parse.
A JavaScript object has very similar syntax to the JSON file format, but is more flexible - the values can be surrounded by double quotes or single quotes, and the keys can have no quotes at all as long as they are valid JavaScript identifiers. If the keys have spaces, dashes, or other non-valid characters, then they need to be surrounded by single quotes or double quotes.
If you need your string to be valid JSON, generate it with JSON.stringify. If it's OK for it to be just valid JavaScript, then it's already fine - it does not matter whether the quotes are there or not.
If, for some reason, you need some imaginary third option (perhaps you are interacting with an API where someone has written their own custom string parser, and they are demanding that all keys are surrounded by single quotes?) you will probably need to write your own little string generator.

Elasticsearch Completion Suggester field contains comma separated values

I have a field that contains comma separated values which I want to perform suggestion on.
{
"description" : "Breakfast,Sandwich,Maker"
}
Is it possible to get only applicable token while performing suggest as you type??
For ex:
When I say break, how can I get only Breakfast and not get Breakfast,Sandwich,Maker?
I have tried using commatokenizer but it seems it does not help
As said in the documentation, you can provide multiple possible inputs by indexing like this:
curl -X PUT 'localhost:9200/music/song/1?refresh=true' -d '{
"description" : "Breakfast,Sandwich,Maker",
"suggest" : {
"input": [ "Breakfast", "Sandwitch", "Maker" ],
"output": "Breakfast,Sandwich,Maker"
}
}'
This way, you suggest with any word of the list as input.
Obtaining the corresponding word as suggestion from Elasticsearch is not possible but as a workaround you could use a tokenizer outside Elasticsearch to split the suggested string and choose only the one that has the input as prefix.
EDIT: a better solution would be to use an array instead of comma-separated values, but it doesn't meet your specs... ( look at this: Elasticsearch autocomplete search on array field )

Couch DB escape characters

I have a list function for CouchDB, but I can't get it into the DB because I'm constantly getting syntax erros. Here is the function:
function(head,req) {
var headers;
start({'headers':{'Content-Type' : 'text/csv; charset=utf-8; header=present'}});
while(r = getRow()) {
if(!headers) {
headers = Object.keys(r.value);
send('"' + headers.join('","') + '"\n');
}
headers.forEach(function(v,i) {
send(String(r.value[v]).replace(/\"/g,'""').replace(/^|$/g,'"'));
(i + 1 < headers.length) ? send(',') : send('\n');
});
}
}
Can anyone show me an example of this function formatted that can be inserted into CouchDB?
List functions are stored in design documents. Design documents are JSON documents, so you need to make sure they conform to the JSON standard. Since List functions are string values in JSON, you need to make sure that you:
Escape any double quotes in the function, so " becomes \". Where possible, you should use single quotes instead of double quotes.
Make sure you replace any line breaks with \n or just remove line breaks as Javascript ignores them anyway.
Escape any backslashes, so \ becomes \\.

Resources