Mornor
Mornor

Reputation: 3783

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 <[email protected]>' ],
  to: [ '<[email protected]>' ] 
}

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.

Upvotes: 0

Views: 136

Answers (1)

stdob--
stdob--

Reputation: 29172

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

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

Upvotes: 2

Related Questions