简体   繁体   中英

How do I parse an object coming back from AWS SES SDK in JavaScript?

I'm trying to use AWS SES getIdentityVerificationAttributes via code, and I'm having trouble parsing the object coming back.

I've made a mess out of the code below, but AWS says it is coming back as a map, so I tried doing a .get() and it threw an error that .get() is not a function. So I tried working with Object.entries but still I can't find a good way of returning the VerificationStatus of this object. I expected JSON.Parse(identityVerify) to work, but it doesn't (getting "ERROR SyntaxError: Unexpected token o in JSON at position 1").

const isEmailAWSVerified = async (Email) => {
    try {
        console.log('[isEmailAWSVerified] Email: ', Email);

   /*
    data = {
        VerificationAttributes: {
        "myEmail@example.com": {
        VerificationStatus: "Success", // Could be "Pending", "Failed"...
        VerificationToken: "EXAMPLE3VYb9EDI2nTOQRi/Tf6MI/6bD6THIGiP1MVY="
        }
      }
    }
   */

        let identityVerify= 
            await SES.getIdentityVerificationAttributes(
                {Identities: [`${Email}`]})
                .promise();
        
        console.log('[isEmailAWSVerified] - identityVerify', identityVerify);
        const parsedResponse = JSON.Parse(identityVerify);

        const ret = Object.entries(identityVerify.VerificationAttributes).map(([key, val]) => {
            let status = '';
            if (key === Email) {
                const tmp = JSON.Parse(val);
                console.log('[isEmailAWSVerified -- Inside Map] tmp: ', tmp);
                // status = Object.entries(val).map(([key, val]) => {
                //     return val;
                // });
                return status;
            }
            return '';
          })

        console.log('[isEmailAWSVerified] - ret', ret);
        
        return ret === 'Success' ? 1 : 0;
    } catch ( err) {
        console.log('[isEmailAWSVerified] - ERROR', err);
        return 0;
    }
}

I found a good example in PHP -- How can I translate that to JS?

try:
            response = self.ses_client.get_identity_verification_attributes(
                Identities=[identity])
            status = response['VerificationAttributes'].get(
                identity, {'VerificationStatus': 'NotFound'})['VerificationStatus']
            logger.info("Got status of %s for %s.", status, identity)
        except ClientError:
            logger.exception("Couldn't get status for %s.", identity)
            raise
        else:
            return status

Actually, we don't need JSON.Parse , because is an object already. We can get verification status just by using const status = identityVerify.VerificationAttributes[ ${Email} ].VerificationStatus;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM