Home > other >  How to extract a value from a JSON document with JS
How to extract a value from a JSON document with JS

Time:06-17

i´n very new working with JSON, and JS, so i got a problem, I´m trying to get the value inside the object attachments[{path = "..."}], but i get multiple errors due I don´t really know how to do it.

This is my JSON:

[{ "from": [{ "address": "[email protected]", "name": "Luis " }], "date": "2022-06-16T14:01:08.000Z", "receivedDate": "2022-06-16T14:01:26.000Z", "to": [{ "address": "[email protected]", "name": "" }], "subject": "Facturacion electronica", "text": "\n", "textPlain": "\n", "hasAttachment": true, "original": { "body": "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" /><div dir=\"ltr\"><div class=\"gmail_default\" style=\"font-family:arial,helvetica,sans-serif;font-size:large\"><br></div></div>\n", "headers": { "received": ["from DM5PR1301MB2218.namprd13.prod.outlook.com (2603:10b6:4:30::30) by MN2PR13MB3693.namprd13.prod.outlook.com with HTTPS; Thu, 16 Jun 2022 14:01:26  0000", "from SN4PR0201CA0046.namprd02.prod.outlook.com (2603:10b6:803:2e::32) by DM5PR1301MB2218.namprd13.prod.outlook.com (2603:10b6:4:30::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5353.11; Thu, 16 Jun 2022 14:01:23  0000", "from SN1NAM02FT0046.eop-nam02.prod.protection.outlook.com (2603:10b6:803:2e:cafe::73) by SN4PR0201CA0046.outlook.office365.com (2603:10b6:803:2e::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5353.14 via Frontend Transport; Thu, 16 Jun 2022 14:01:23  0000", "from mail-pg1-f182.google.com (209.85.215.182) by SN1NAM02FT0046.mail.protection.outlook.com (10.97.5.4) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5353.14 via Frontend Transport; Thu, 16 Jun 2022 14:01:22  0000", "by mail-pg1-f182.google.com with SMTP id d129so1314505pgc.9 for <[email protected]>; Thu, 16 Jun 2022 07:01:22 -0700 (PDT)"], "authentication-results": "spf=pass (sender IP is 209.85.215.182) smtp.mailfrom=gmail.com; dkim=pass (signature was verified) header.d=gmail.com;dmarc=pass action=none header.from=gmail.com;compauth=pass reason=100", "received-spf": "Pass (protection.outlook.com: domain of gmail.com designates 209.85.215.182 as permitted sender) receiver=protection.outlook.com; client-ip=209.85.215.182; helo=mail-pg1-f182.google.com; pr=C", "dkim-signature": "v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:from:date:message-id:subject:to; bh=vQCl0MdNDi0YVPYmwIcLob2sZQ71Mebd3Yrxzhr2nV8=; b=hOasXO3PARCRh6q OUeBy9jgqQMeCXFnFtqECYaqBHWLIMMEhmsEASC3TgBGD/e2aT ssZZWLPMebxtvH/MlywEDSpAT7XIaEPdng94OXrUeIFc3LeGwxc9XYpVqM4AYn3fIGko cxP9anbIq X4a0wP5h8QZha4lR4ljPlsVFwk9vNMeEsjxcvdpeUR0wgFo2S5jLYx6dIp UoNKHM5 jtcYLvIozU9Jyo0SjZhTy6HjEQGBgLCq2qgliCsrJdr2U2jg1KvMfgCwe/ML sfsyCxxicz3k5vd7alNEui1sbCEvNw8NnKjOyuXygKQo3DOvDrR3sO4DGOpjaJTMUjlt nqsA==", "x-google-dkim-signature": "v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=vQCl0MdNDi0YVPYmwIcLob2sZQ71Mebd3Yrxzhr2nV8=; b=CDJrrRsU7P8GE Wt91DoNru2dF7J2CBccSM7ST/dy N7DJ3laHstwWF4qGcc/jdHXb yKDcUnA8tEH SAlI qMSVh2w60uyT7kVMKqMp9Pxf6oUj3ARanktSic0aZcVr/HN9kfh cu4FcU8AlUl dSNg dQ8fbmjvinuiMk1o2NG08HiXq9H5NQDMrss70sU7LA yHqp0j9r  FrpsyAPlki58Kfj7MonmoqBSm9P5MCxVDRDp/N8lz0KCpMor5mXIUsb3Qb0F9YY97Ly 1Dfu31FmBSD2iVcYQ73xyEcpFNLRk22PiHHfBjmtkggRm5HvcPeqE8irblzRSP5zQoLX RAVw==", "x-gm-message-state": "AJIora bCFuSkz5hlTY5ZdIyetiVpD0KGMzS4fcKCNGoEhA2qgQ9vp1G LtSF7qtu89FIJkcLfM3fasLPL8YL2M8KURKW5DCFUgbcurU=", "x-google-smtp-source": "AGRyM1vNXatnK7wAXbgyqbI4O/wuFK BQ3cfP1jz3ra3mzWl/22XO5Q4YDaFGEZ4aq0pRFI/bFjqHnOWBWd1NauKNfo=", "x-received": "by 2002:a63:1b53:0:b0:3fd:168e:d9a with SMTP id b19-20020a631b53000000b003fd168e0d9amr4681929pgm.617.1655388081210; Thu, 16 Jun 2022 07:01:21 -0700 (PDT)", "from": "Luis  <[email protected]>", "date": "Thu, 16 Jun 2022 09:01:08 -0500", "message-id": "<CAL0BCDnijtvACn6Y408hX6 XDRih6zKg [email protected]>", "subject": "Facturacion electronica", "to": "[email protected]", "content-type": "multipart/mixed; boundary=\"0000000000008bea1805e191122e\"", "return-path": "[email protected]", "x-ms-exchange-organization-expirationstarttime": "16 Jun 2022 14:01:22.8465 (UTC)", "x-ms-exchange-organization-expirationstarttimereason": "OriginalSubmit", "x-ms-exchange-organization-expirationinterval": "1:00:00:00.0000000", "x-ms-exchange-organization-expirationintervalreason": "OriginalSubmit", "x-ms-exchange-organization-network-message-id": "08cd21ef-317f-4b77-cff7-08da4fa0b224", "x-eopattributedmessage": "0", "x-eoptenantattributedmessage": "3855fd0e-2e9a-4fcc-9552-878a0fe4a05d:0", "x-ms-exchange-organization-messagedirectionality": "Incoming", "x-ms-publictraffictype": "Email", "x-ms-exchange-organization-authsource": "SN1NAM02FT0046.eop-nam02.prod.protection.outlook.com", "x-ms-exchange-organization-authas": "Anonymous", "x-ms-office365-filtering-correlation-id": "08cd21ef-317f-4b77-cff7-08da4fa0b224", "x-ms-traffictypediagnostic": "DM5PR1301MB2218:EE_", "x-ms-exchange-organization-scl": "1", "x-microsoft-antispam": "BCL:0;", "x-forefront-antispam-report": "CIP:209.85.215.182;CTRY:US;LANG:es;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail-pg1-f182.google.com;PTR:mail-pg1-f182.google.com;CAT:NONE;SFS:(13230016)(84050400002)(42186006)(1096003)(4006050)(6916009)(5660300002)(76482006)(356005)(7636003)(21480400003)(8676002)(86362001)(7596003)(7116003)(21470400002)(75640400001)(73392003)(55446002)(33964004)(26005)(336012)(82202003)(3480700007)(6666004)(564344004)(563144003);DIR:INB;", "x-ms-exchange-crosstenant-originalarrivaltime": "16 Jun 2022 14:01:22.7840 (UTC)", "x-ms-exchange-crosstenant-network-message-id": "08cd21ef-317f-4b77-cff7-08da4fa0b224", "x-ms-exchange-crosstenant-id": "3855fd0e-2e9a-4fcc-9552-878a0fe4a05d", "x-ms-exchange-crosstenant-authsource": "SN1NAM02FT0046.eop-nam02.prod.protection.outlook.com", "x-ms-exchange-crosstenant-authas": "Anonymous", "x-ms-exchange-crosstenant-fromentityheader": "Internet", "x-ms-exchange-transport-crosstenantheadersstamped": "DM5PR1301MB2218", "x-ms-exchange-transport-endtoendlatency": "00:00:03.4778129", "x-ms-exchange-processed-by-bccfoldering": "15.20.5353.014", "x-microsoft-antispam-mailbox-delivery": "ucf:0;jmr:0;auth:0;dest:I;ENG:(910001)(944506458)(944626604)(920097)(930097);", "x-microsoft-antispam-message-info": "rheidSJ3gyr1XepOaJ17S9LjRMc9FjOc8VPhMFGDvlLrCoHayxpaJN2Xe /79unbyGbEPqNlKy ouyW/kWiWbMjtkimlHzHZDBB3456/JLb3xkUdbuBmNwBvLvuPQ 9nqW1c1q9zHryvsGAAWnzn0CS8CZAziQjbABYTxkKLPDe0K0vTh Un0e3x kQgveI8CQE9UPgejQn8lAtcPD0DcAt6OB92NrlcEGlG7oEuPkTrZg1P5zVFaN/mC1F1FC0uujUcq/qCjV6wRWiN 5xRDpwMuJBdW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BogmrCc6WtCrXrqBbmYipu2vhsn73cd5RWJNUltifwlnL3v3QSXRyx6xiExI5IF6j7dn1fBn2jDLcEZWc5D1WPajgGu9y2ZWYXzblwya8ZY0RW28qBWuxqjkOf34tF6hJTaXcSlbg7936dtoor2WA6Cd0CUDuShP vny9uOcXWQk6Oh59gxc56pk3rZoWGQ4tCK5q0=", "mime-version": "1.0" } }, "uid": "2243", "attachments": [{ "path": "C:\\Users\\usuario\\Desktop\\ioio\\Proyectos\\ioio\\2022-06-16\\1655388696749.oioio.zip" }] }]

The attachments part, is at the end of the JSON, and this is what I´m trying from JS

const jsonData = require('./prueba.json');
var strngJson = JSON.parse(JSON.stringify(jsonData))
console.log(strngJson)
console.log(jsonData.attachments.path);
//console.log(data.attachments.path[0]);

As i said, i´m really new with JS; son any explanation on how it will work, will be really appreciate it! Thanks a lot

CodePudding user response:

I think your problem is that you are getting an array from that json and then you have the same problem again, I mean the attachments is another array. So you will have to do the following:

const jsonData = require("./prueba.json");
var strngJson = JSON.parse(JSON.stringify(jsonData));
console.log(strngJson);
console.log(strngJson[0].attachments[0].path);
  • Related