Mornor Mornor - 7 months ago 11
Javascript Question

Parse specific String Javascript (Node.js)

I would like to parse the following String:

{
date: [ 'Thu, 28 Apr 2016 10:56:13 +0200' ],
subject: [ 'Subject' ],
from: [ 'Blob <blob@test.com>' ],
to: [ '<blab@test.com>' ]
}


In order to access the variable
date
,
subject
etc ...

But I am not sure how to do it since it


  1. Is not a valid JSON

  2. It is not a structure I know



And I don't want to re-invent the wheel if a solution exist which I am not (yet) aware of.

Any ideas?

EDIT

Data are getting using a node-imap module (only relevant part)

f.on('message', function(msg, seqno) {
console.log('Message #%d', seqno);
var prefix = '(#' + seqno + ') ';
msg.on('body', function(stream, info) {
var buffer = '';
stream.on('data', function(chunk) {
buffer += chunk.toString('utf8');
});
stream.once('end', function() {
var parsedHeader = inspect(Imap.parseHeader(buffer));
console.log('Author: '+parsedHeader);
});


SOLVED

See the comment of @stdob--. Imap.parseHeader() return an object.

Answer

Looks like that Imap.parseHeader already returns an object with keys

Try console.log( Object.keys(parsedHeader) to see all the keys.