How to purify the output of created structured json file in javascript? - javascript

I learned how to create structured json file from csv file that generated from google form. However, produced json object contains time header because google form generates that and it saved to csv before creating json object from it.
Here is what I tried to solve this:
let inputCsv = `"Timestamp","Enter First Name:","Enter Middle Initial","Enter Last Name:","Enter UIN:","Are you attending the event?"
"2019/02/22 12:41:56 PM CST","Jonathan","Samson,"Rowe","670168228","No"
"2019/02/22 12:44:56 PM CST","Phil","Aspilla","beltran","6689144343","Yes"`
function convertToJson(inputCsv) {
//inputCsv passed from readfile function
/* Split input string by `,` and clean up each item */
var lines = inputCsv.split('\n');
lines.splice(0,1);
var arrayCsv = [];
for (var line = 0; line < lines.length; line++){
const lineArray = lines[line].split(',').map(s => s.replace(/"/gi, '').trim());
lineArray.splice(0,1);
arrayCsv[line] = lineArray;
}
const outputJson = [];
console.log(arrayCsv);
}
convertToJson(inputCsv)
basically I want to get rid of extra time header (like `2019/02/22 12:41:56 PM CST) every line of input csv file when I create structured json object from it, so here is my desired output:
update: desired output json file
[{
"uin": "123456789",
"studentInfo": {
"firstName": "Jonathan",
"middleName": "Samson",
"lastName": "Rowe",
"rsvpStatus": "Yes"
}
},
{
"uin": "123456788",
"studentInfo": {
"firstName": "phil",
"middleName": "Aspilla",
"lastName": "beltran",
"rsvpStatus": "No"
}
}]
I am not sure how regular expression works in javascript and I want to produce clean structured json object from csv that comes from google form. How can I make this happen? Any way to do this? Thanks

You need to build object in desired format and than push in array.
let inputCsv = `"Timestamp","Enter First Name:","Enter Middle Initial","Enter Last Name:","Enter UIN:","Are you attending the event?"
"2019/02/22 12:41:56 PM CST","Christ","A","coda","670168228","No"
"2019/02/22 12:44:56 PM CST","jack","NA","Harvey","6689144343","Yes"`
function convertToJson(inputCsv) {
const keyMapping = {
"\"Enter First Name:\"" : 'firstName',
"\"Enter Middle Initial\"": 'middleName',
"\"Enter Last Name:\"": 'lastName',
"\"Enter UIN:\"":'uin',
"\"Are you attending the event?\"":'rsvp'
}
var lines = inputCsv.split('\n');
let [keys] = lines
keys = keys.split(',').splice(1,)
lines.splice(0,1);
var arrayCsv = [];
for (var line = 0; line < lines.length; line++){
const lineArray = lines[line].split(',').splice(1,).reduce((o,s,i) =>((o[keyMapping[keys[i]]] = s.replace(/"/gi, '').trim()),o),{});
// here i am creating object in desired format
let obj = {uin:lineArray['uin'],studentInfo:{firstName:lineArray}}
arrayCsv[line] = obj;
}
const outputJson = [];
console.log(arrayCsv);
}
convertToJson(inputCsv)

Related

How to add new keys and values to a json from a txt file in node.js

From a txt file with values but not keys I want to add those values to a empty json file. I have to add the key for each value, save the json file and export the json file to a new txt, this time with keys.
So I have this json file
{ "agenda" : []
}
I have this txt file (all fake data in case you wonder):
"Fidel","Oltra","fidel#gmail.com","6650403234"
"Merxe","Sanz","merxe#gmail.com","65345235"
"David","Garcia","dgarcia#gmail.com","69823422"
"Amparo","López","alopez#gmail.com","67234234"
"Antonio","Gómez","antoniog#gmail.com","69929292"
And I want the json file to look like
{
"agenda":[
{
"Name": "Fidel",
"Surname": "Oltra",
"Email": "fidel#gmail.com",
"Phone": 6650403234
},
{
...
},
...
]
}
I have this code that is kind of working but I don't know how to push properly the data because at the final json file doesn't look as expected.
const archivo = require("fs");
const json = require("fs");
const file = archivo.readFileSync('agenda.txt', 'utf8');
console.log(file);
const lines = file.split('\n');
console.log(lines);
let campos;
let rawdata = json.readFileSync('agenda.json');
let json1 = JSON.parse(rawdata);
console.log(json1);
for (i in lines) {
campos = lines[i].split(",");
json1.agenda.push('Nombre:', campos[0]);
json1.agenda.push('Apellido:', campos[1]);
json1.agenda.push('Email:', campos[2]);
json1.agenda.push('Teléfono:', campos[3]);
console.log(campos);
console.log(json1);
};
let data = JSON.stringify(json1);
json.writeFileSync('agenda2.json', data);
And the json when I open it is:
{"agenda":["Nombre:","\"Fidel\"","Apellido:","\"Oltra\"","Email:","\"fidel#gmail.com\"","Teléfono:","\"6650403234\"\r","Nombre:","\"Merxe\"","Apellido:","\"Sanz\"","Email:","\"merxe#gmail.com\"","Teléfono:","\"65345235\"\r","Nombre:","\"David\"","Apellido:","\"Garcia\"","Email:","\"dgarcia#gmail.com\"","Teléfono:","\"69823422\"\r","Nombre:","\"Amparo\"","Apellido:","\"López\"","Email:","\"alopez#gmail.com\"","Teléfono:","\"67234234\"\r","Nombre:","\"Antonio\"","Apellido:","\"Gómez\"","Email:","\"antoniog#gmail.com\"","Teléfono:","\"69929292\""]}
So I would like some help to make it work and learn what I am doing wrong... and also to know how to write the right final json back to txt file.
There's two issues I found.
You never removed the quotes / cotizaciones from agenta.txt. This means that your ending json was escaping the quotes for you. "String" in JSON is "\"String\"".
In the for loop, you were pushing your keys and values into an array instead of making an object, and then pushing that object in the array.
const fs = require("fs");
const file = fs.readFileSync("agenda.txt", "utf8");
console.log(file);
const sinCotiz = file.replace(/"/g, ""); // remove quotes
const lines = sinCotiz.split("\n");
console.log(lines);
const rawdata = fs.readFileSync("agenda.json");
const json1 = JSON.parse(rawdata);
console.log(json1);
for (i in lines) {
const obj = {}; // create an object
const campos = lines[i].split(",");
obj["Nombre"] = campos[0]; // create keys and values for the object
obj["Apellido"] = campos[1];
obj["Email"] = campos[2];
obj["Teléfono"] = campos[3];
json1.agenda.push(obj); // push the entire object in the array
console.log(campos);
console.log(json1);
}
const data = JSON.stringify(json1);
fs.writeFileSync("agenda2.json", data);
i think there is more simple solution,
but you can format your file as it is a CSV file . you will put a layer at first to remove the '' and change , with ; , then you will need to add a header line in your case the header will be
Nombre;Apellido;Email;Teléfono
then you use this function :
function csvJSON(csv){
var lines=csv.split("\n");
var result = [];
var headers=lines[0].split(",");
for(var i=1;i<lines.length;i++){
var obj = {};
var currentline=lines[i].split(",");
for(var j=0;j<headers.length;j++){
obj[headers[j]] = currentline[j];
}
result.push(obj);
}
//return result; //JavaScript object
return JSON.stringify(result); //JSON
}
this will gives you a formated json with the following strecture :
[{Nombre:4444,Apellido:"AAAA",....}]
and you can put the whole function directly to your agenda :)
again i think there is more simple solution , but maybe this will help.
const archivo = require("fs");
const json = require("fs");
const file = archivo.readFileSync('agenda.txt', 'utf8');
const lines = file.split('\n');
let campos;
let rawdata = json.readFileSync('agenda.json');
let json1 = JSON.parse(rawdata);
for (i in lines) {
if(lines[i]) {
campos = lines[i].split(",");
let payload = {
'Name' : campos[0].substr(1,campos[0].length-2), // to remove double quotes from the string
'Surname' : campos[0].substr(1,campos[0].length-2),
'Email' : campos[0].substr(1,campos[0].length-2),
'Phone' : campos[0].substr(1,campos[0].length-2),
}
json1.agenda.push(payload);
}
};
let data = JSON.stringify(json1);
json.writeFileSync('agenda2.json', data);

How to resolve content service for google app - script return java script object not json string?

I am using google spread-sheet as a database for my application. I want that spreadsheet data to return as a JSON response like GET API. I have written the below code for getting the response.
function doGet(request){
// Open Google Sheet using ID
var sheet = SpreadsheetApp.openById("sheetid");
// Get all values in active sheet
var values = sheet.getActiveSheet().getDataRange().getValues();
var data = [];
for (var i = 1; i <= values.length-1; i++) {
var row = values[i];
var feedback = {};
feedback['timestamp'] = row[1];
feedback['name'] = row[2];
feedback['designation'] = row[3];
feedback['blood_group'] = row[4];
data.push(feedback);
}
return ContentService
.createTextOutput(JSON.stringify(data))
.setMimeType(ContentService.MimeType.JSON);
}
I want my response like this :
[{
"timestamp": "2020-08-03T12:43:38.662Z",
"name": "Mehedi Hasan",
"designation": "developer",
"blood_group": "o-"
}]
but, it's giving response like this:
[{
timestamp: "2020-08-03T12:43:38.662Z",
name: "Mehedi Hasan",
designation: "developer",
blood_group: "o-"
}]
It's returning Javascript object not in JSON string even though I have used JSON.stringify() function to convert it. I don't understand if there is any mistake I have done here? Any suggestion will be helpful.

How can I Merge an array of objects

I was wondering how i could merge these two objects retrieve the tag values and store them in an array. This data is also coming from a json response so incoming data should be pushed onto the end of the new array.
so it would look something like this
["2011 LDI", "2012 LDI"]
array with incoming data:
["2011 LDI", "2012 LDI","2013 LDI"]
Here is what I am getting back in my console.log:
[19-08-25 21:58:32:055 PDT] []
[19-08-25 21:58:32:056 PDT] []
Here are the two objects of arrays i am trying to merge:
{date_added=2019-08-26 04:19:00.112083, tag=LDI 2011}
{date_added=2019-08-26 04:19:00.112089, tag=LDI 2012}
and I want it to look like this
[LDI 2011, LDI 2012]
and how I am trying to do it.
var tagtest = [];
var tags = message.student_detail.student_tags,
i = 0,
len = tags.length;
for (i; i < len; i++) {
var obj = tags[i];
for (a in obj) {
}
Array.prototype.push(tags, tagtest);
Logger.log(tagtest)
}
Based on your desired output ([LDI 2011, LDI 2012]), You may want the only tag values from the array, If this is what you are looking for then .map() will help you
const array = [
{
date_added: '2019-08-26',
tag: 'LDI 2011'
},
{
date_added: '2019-08-26',
tag: 'LDI 2012'
}];
const tags = array.map((r) => {
const chunk = r.tag.split(' ');
return `${chunk[1]} ${chunk[0]}`;
} );
console.log(tags);
A for in loop is a great way to work with objects. I updated the code above so that it was actually an array of objects, not an error. See below :)
var data = [{date_added: "2019-08-26 04:19:00.112083", tag: "LDI 2011"},
{date_added: "2019-08-26 04:19:00.112089", tag: "LDI 2012"}];
var newArr = [];
for(var item in data) {
newArr.push(data[item].tag);
}
console.log(newArr);

Converting xml request to json request with duplicate node names

I trying to push data to a 3rd party webservice, specifically converting the xml request to a json one (for use with node soap).
Here is an example of the raw xml request that works fine:
<EformData>
<EformFields>
<FieldName>txt_customername</FieldName>
<FieldValue>Scott</FieldValue>
</EformFields>
<EformFields>
<FieldName>txt_organisation</FieldName>
<FieldValue>My Orginisation</FieldValue>
</EformFields>
<EformFields>
<FieldName>txt_address</FieldName>
<FieldValue>My Address</FieldValue>
</EformFields>
<EformFields>
<FieldName>txt_telnumber</FieldName>
<FieldValue>123456</FieldValue>
</EformFields>
</EformData>
The problem i'm having is trying to convert these duplicate nodes into an object, the new object data is being overwritten with the last request.
Here's what i have so far:
var formValues = {
"txt_customername": "Scott",
"txt_organisation": "My Orginisation",
"txt_address": "My Address",
"txt_telnumber": "123456"
}
// Container
var EformData = {
"EformFields": {
}
};
// populate the object
for (var key in formValues) {
EformData.EformFields.FieldName = [key];
EformData.EformFields.FieldValue = formValues[key];
}
As you can see below, only the last request is stored in the object, the others are overwritten:
<EformData>
<EformFields>
<FieldName>txt_telnumber</FieldName>
<FieldValue>123456</FieldValue>
</EformFields>
</EformData>
Is it possible to build an object in such a way to match the orginal duplicate xml node data?
The data structure of your json should be that EformData has an array of EformFields objects, which has the properties of FieldName and FieldValue.
var formValues = {
"txt_customername": "Scott",
"txt_organisation": "My Orginisation",
"txt_address": "My Address",
"txt_telnumber": "123456"
}
// Container
var EformData = {
"EformFields": []
};
// populate the object
for (var key in formValues) {
EformData.EformFields.push({
"FieldName": key,
"FieldValue": formValues[key]
});
}
In your array, only 0th index is populated always and hence it is overrided when on the next iteration add an index for the array iteration as follows
// Container
var EformData = {
"EformFields": [
]
};
// populate the object
int i=0;
for (key in formValues) {
EformData.EformFields[i].FieldName = [key];
EformData.EformFields[i].FieldValue = formValues[key];
i++;
}

Nodejs - Writing a tab delimited file as json object

Is there a npm module which converts a tab delimited file to a JSON object so i could look up the data by certain properties.
Example: The file would looks like below,
name sex age
A M 20
B F 30
C M 40
D F 50
JSON
[
{
"name": "A",
"sex": "M",
"age": "20"
},
{
"name": "B",
"sex": "F",
"age": "30"
},
/* Etc. */
]
Sometimes i prefer not using node modules so that I can run these scripts without any setup....
IE. nodejs ./convert-to-csv.js
Here is a nodejs script in case you choose not to use a node module.
var fs = require("fs");
fs.readFile("./birthrate_poverty.txt","utf8", function(err, data){
var rows = data.split("\n");
var json = [];
var keys = [];
rows.forEach((value, index)=>{
if(index < 1){// get the keys from the first row in the tab space file
keys = value.split("\t");
}else {// put the values from the following rows into object literals
values = value.split("\t");
json[index-1] = values.map((value, index) => {
return {
[keys[index]]: value
}
}).reduce((currentValue, previousValue) => {
return {
...currentValue,
...previousValue
}
});
}
})
// convert array of objects into json str, and then write it back out to a file
let jsonStr = JSON.stringify(json);
fs.writeFileSync("./birthrate_poverty.json", jsonStr, {encoding: "utf8"})
});
Yes, csvtojson and the delimiter can be anything not only commas.
Example:
const csvFilePath='FILE'
const csv=require('csvtojson')
csv({delimiter:"\t"})
.fromFile(csvFilePath)
.on('json',(jsonObj)=>{
console.log(jsonObj);
})
.on('done',(error)=>{
console.log('end');
})

Categories