Thomas L
Thomas L

Reputation: 330

NodeJS: retrieving an array which is wrapped in a string

I'm currently trying to save a mail object () I'm getting back from an API into our Mongo database. This is all fine for all values being passed accept the field 'attachments'. The attachments are supposed to be an array of objects, but this is wrapped in a string. My question now is: how do I get the array out of that string?

Example of the req.body which I'm getting from the POST request:

{ timestamp: '1538728432', token: '0a1336769443d5a593d0ef7c1c817d29ef64ba422fc3cc45f1', signature: 'c44c67660f0ad97bbb5ece92c733302d4511d4607b5787ae6aa8f2203b0e2e3e',
domain: 'karaton.uk', From: '',
'X-Envelope-From': '', To: '', 'Dkim-Signature': 'v=1; a=rsa-sha256; c=relaxed/relaxed;\td=karaton.be; s=20140924;\th=content-type:subject:reply-to:from:to:message-id:mime-version:date:from;\tbh=Xfm+LiX4NVMKTRaQB7gNUXf6z8yVjUqn23aenMee/q4=;\tb=JEslFgXEsO3/kW3khZFF6F4HEo9Mwx+oXJH5DGsXALaGa98ki9o5tLddmEcClqhZ8IJLR0+vrMJ3x\t zU5ThqHNElHpt6VE9MX5ZSHJHnI2kiEXq7MN5BUWfStlS2stYwooZcuZKOkbZXRaUHODewUQbhvZQD\t 5/eA5RltbAOqwIaA=', from: '', 'User-Agent': 'One.com webmail 27.8.5', Date: 'Fri, 05 Oct 2018 10:21:49 +0200',
'Message-Id': '<1538727709180.51726.8212@webmail12>',
'Mime-Version': '1.0', Received: [ 'from mailrelay4-2.pub.mailoutpod1-cph3.one.com (mailrelay4-2.pub.mailoutpod1-cph3.one.com [46.30.212.3]) by mxa.mailgun.org with ESMTP id 5bb71f1f.7f030c2a1580-smtp-in-n02; Fri, 05 Oct 2018 08:21:51 -0000 (UTC)', 'from onecom-webmail2 94.143.189.242 (unknown [46.30.211.130])\tby mailrelay4.pub.mailoutpod1-cph3.one.com (Halon) with ESMTPSA\tid b4d6415f-c877-11e8-86b7-d0431ea8bb10;\tFri, 05 Oct 2018 08:21:49 +0000 (UTC)' ], 'X-Halone-Cookie': 'c9ea0f7e80128b5bfaf5bf8d8aca6fced5bcb162', subject: '2 attachments', 'message-url': 'https://se.api.mailgun.net/v3/domains/karaton.uk/messages/eyJwIjpmYWxzZSwiayI6IjEyNzQ1ZmViLTM4OTItNDdkZC04MTJiLWJlYzU4ZjNjYTI4MCIsInMiOiI1NDE1ODQ0ZjIyIiwiYyI6InRhbmtiIn0=', 'Reply-To': '', recipient: '[email protected]',
sender: '[email protected]', 'X-Mailgun-Incoming': 'Yes',
'message-headers': '[["X-Mailgun-Incoming", "Yes"], ["X-Envelope-From", ""], ["Received", "from mailrelay4-2.pub.mailoutpod1-cph3.one.com (mailrelay4-2.pub.mailoutpod1-cph3.one.com [46.30.212.3]) by mxa.mailgun.org with ESMTP id 5bb71f1f.7f030c2a1580-smtp-in-n02; Fri, 05 Oct 2018 08:21:51 -0000 (UTC)"], ["Dkim-Signature", "v=1; a=rsa-sha256; c=relaxed/relaxed;\td=karaton.be; s=20140924;\th=content-type:subject:reply-to:from:to:message-id:mime-version:date:from;\tbh=Xfm+LiX4NVMKTRaQB7gNUXf6z8yVjUqn23aenMee/q4=;\tb=JEslFgXEsO3/kW3khZFF6F4HEo9Mwx+oXJH5DGsXALaGa98ki9o5tLddmEcClqhZ8IJLR0+vrMJ3x\t zU5ThqHNElHpt6VE9MX5ZSHJHnI2kiEXq7MN5BUWfStlS2stYwooZcuZKOkbZXRaUHODewUQbhvZQD\t 5/eA5RltbAOqwIaA="], ["X-Halone-Cookie", "c9ea0f7e80128b5bfaf5bf8d8aca6fced5bcb162"], ["X-Halone-Id", "b4d6415f-c877-11e8-86b7-d0431ea8bb10"], ["Received", "from onecom-webmail2 94.143.189.242 (unknown [46.30.211.130])\tby mailrelay4.pub.mailoutpod1-cph3.one.com (Halon) with ESMTPSA\tid b4d6415f-c877-11e8-86b7-d0431ea8bb10;\tFri, 05 Oct 2018 08:21:49 +0000 (UTC)"], ["X-Originating-Ip", "94.143.189.242"], ["User-Agent", "One.com webmail 27.8.5"], ["Date", "Fri, 05 Oct 2018 10:21:49 +0200"], ["Mime-Version", "1.0"], ["Message-Id", "<1538727709180.51726.8212@webmail12>"], ["To", ""], ["From", ""], ["Reply-To", ""], ["Subject", "2 attachments"], ["Content-Type", "multipart/mixed; boundary=\"----------8210-1538727709180-1\""]]', 'X-Halone-Id': 'b4d6415f-c877-11e8-86b7-d0431ea8bb10', 'Content-Type': 'multipart/mixed; boundary="----------8210-1538727709180-1"',
'X-Originating-Ip': '94.143.189.242', Subject: '2 attachments',
attachments: '[{"url": "https://se.api.mailgun.net/v3/domains/karaton.uk/messages/eyJwIjpmYWxzZSwiayI6IjEyNzQ1ZmViLTM4OTItNDdkZC04MTJiLWJlYzU4ZjNjYTI4MCIsInMiOiI1NDE1ODQ0ZjIyIiwiYyI6InRhbmtiIn0=/attachments/0", "content-type": "application/pdf", "name": "WOORDZOEKER maken _ Online _ Met oplossing2 copy.pdf", "size": 53398}, {"url": "https://se.api.mailgun.net/v3/domains/karaton.uk/messages/eyJwIjpmYWxzZSwiayI6IjEyNzQ1ZmViLTM4OTItNDdkZC04MTJiLWJlYzU4ZjNjYTI4MCIsInMiOiI1NDE1ODQ0ZjIyIiwiYyI6InRhbmtiIn0=/attachments/1", "content-type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "name": "Geschatte uren.xlsx", "size": 44210}]', 'body-plain': '\r\n\r\n', 'body-html': '
\r\n', 'stripped-html': '
\n', 'stripped-text': '', 'stripped-signature': '' }

Upvotes: 0

Views: 125

Answers (2)

deerawan
deerawan

Reputation: 8443

JSON.parse should do the trick

const response = {
  timestamp: '1538728432',
  token: '0a1336769443d5a593d0ef7c1c817d29ef64ba422fc3cc45f1',
  Subject: '2 attachments',
  attachments: '[{"url": "https://se.api.mailgun.net/v3/domains/karaton.uk/messages/eyJwIjpmYWxzZSwiayI6IjEyNzQ1ZmViLTM4OTItNDdkZC04MTJiLWJlYzU4ZjNjYTI4MCIsInMiOiI1NDE1ODQ0ZjIyIiwiYyI6InRhbmtiIn0=/attachments/0", "content-type": "application/pdf", "name": "WOORDZOEKER maken _ Online _ Met oplossing2 copy.pdf", "size": 53398}, {"url": "https://se.api.mailgun.net/v3/domains/karaton.uk/messages/eyJwIjpmYWxzZSwiayI6IjEyNzQ1ZmViLTM4OTItNDdkZC04MTJiLWJlYzU4ZjNjYTI4MCIsInMiOiI1NDE1ODQ0ZjIyIiwiYyI6InRhbmtiIn0=/attachments/1", "content-type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "name": "Geschatte uren.xlsx", "size": 44210}]'
}

const attachments = JSON.parse(response.attachments);
console.log(attachments[0]);
console.log(attachments[1]);

Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

Upvotes: 0

Milan Velebit
Milan Velebit

Reputation: 2027

Use the built-in JSON.parse() function, it'll return an array of objects as it should. It's opposed to JSON.stringify(), which does the opposite. So, basically, JSON.parse(req.body.attachments) should do the trick.

Upvotes: 0

Related Questions