Reputation: 47
So I have a Nodejs server and it receives a JSON object then puts it in a variable and emails that content out. Here is my code:
app.post('/sendEmail', function(req, res) {
var answers = req.body.answers;
var str = JSON.stringify(answers, null, "\t"); // stringify with tabs inserted at each level
console.log(answers);
var fromEmail = new helper.Email('ahun...ok.com');
var toEmail = new helper.Email('ahun...ok.com');
var subject = 'Survey Completion';
var content = new helper.Content('text/plain', str);
var mail = new helper.Mail(fromEmail, subject, toEmail, content);
var sg = require('sendgrid')(process.env.SENDGRID_API_KEY);
var request = sg.emptyRequest({
method: 'POST',
path: '/v3/mail/send',
body: mail.toJSON()
});
sg.API(request, function (error, response) {
if (error) {
console.log('Error response received');
}
});
res.send("Success");
});
As you can see, I am using stringify to make the JSON more pretty, but it still has all the gross brackets and quotes and spacing as JSON. Is there a way to store the JSON in a more readable form?
The email content is here:
var content = new helper.Content('text/plain', str);
So the readable JSON needs to be stored in a variable with formatting and such.
Any help would be appreciated. Thanks
EDIT: here is the JSON object being sent over:
{
"question1": {
"Reliable": true,
"Knowledgeable": true,
"Helpful": true,
"Courteous": true
},
"question2": {
"checked": "Extremely Well"
},
"question3": {
"checked": "Extremely Well"
},
"question4": {
"checked": 3
},
"fullName": "Test",
"address": "Test",
"city": "Test",
"state": "Test",
"zip": "321",
"areaCode": "321",
"phone": 1234567896,
"call": true,
"lifeInsurance": "Yes",
"brokerage": "Yes",
"bankName": "Regions"
}
Using the above JSON object, I would like to format it to be like so:
Question 1: Reliable, Knowledgable, Helpful, Courteous.
Question 2: Extremely Well.
Question 3: Extremely Well.
Question 4: 3.
Full Name: Test.
Address: Test.
....
Brokerage: Yes.
Bank Name: Regions.
Upvotes: 1
Views: 2831
Reputation: 5983
An even easier answer might be parsing the JSON then dumping it as YAML:
---
question1:
Reliable: true
Knowledgeable: true
Helpful: true
Courteous: true
question2:
checked: Extremely Well
question3:
checked: Extremely Well
question4:
checked: 3
fullName: Test
address: Test
city: Test
state: Test
zip: '321'
areaCode: '321'
phone: 1234567896
call: true
lifeInsurance: 'Yes'
brokerage: 'Yes'
bankName: Regions
There are several YAML libraries for Node.js, and I don't remember which I like, but I want to say I have had pretty good luck with js-yaml.
Upvotes: 0
Reputation: 1
The data just needs a bit of massaging
var data = {
"question1": {
"Reliable": true,
"Knowledgeable": false,
"Helpful": true,
"Courteous": true
},
"question2": {
"checked": "Extremely Well"
},
"question3": {
"checked": "Extremely Well"
},
"question4": {
"checked": 3
},
"fullName": "Test",
"address": "Test",
"city": "Test",
"state": "Test",
"zip": "321",
"areaCode": "321",
"phone": 1234567896,
"call": true,
"lifeInsurance": "Yes",
"brokerage": "Yes",
"bankName": "Regions"
};
var result = Object.entries(data).reduce((result, [key, value]) => {
key = key.replace(/([A-Z]|\d+)/g, ' $1').replace(/^(.)/, (unused, p1) => p1.toUpperCase());
if (!['string', 'number', 'boolean'].includes(typeof value)) {
value = Object.entries(value).map(([key, value]) => (typeof value == 'boolean') ? (value ? key : undefined) : value).filter(v => v !== undefined).join(',');
}
result.push(`${key}: ${value}`);
return result;
}, []);
console.log(result.join('\n'));
Note: I changed one of the true to false to check the logic
Tested in node 8.1.2
Upvotes: 2