Skip to content Skip to sidebar Skip to footer

How To Extract Fields From This Aws Secretsmanager Json Object?

I am using AWS Secrets manager to protect the database credits of my REST API. I am using AWS Lambda, API Gateway and RDS (MySQL). Below is how I get them. // Load the AWS SDK var

Solution 1:

At first, you get back secret as data.SecretString, then now secret just is a normal string. In your case, it is a JSON string, you must cast your string to a JSON object, then you can access the information by attribute name easily.

To do that, you can use JSON.parse method to convert a json string to json object:

var secret = `{"username":"***","password":"***","engine":"mysql","host":"***.***.us-east-1.rds.amazonaws.com","port":3306,"dbname":"***","dbInstanceIdentifier":"***"}
`;

const secretObj = JSON.parse(secret);

console.log(secretObj.host)

Solution 2:

This Output is a JSON object literal so You Can access each object with bellow format :

secret.password

const secret= {"username":"yourusername","password":"Pa$$w0rd","engine":"mysql","host":"***.***.us-east-1.rds.amazonaws.com","port":3306,"dbname":"***","dbInstanceIdentifier":"***"};
console.log(secret.password);
console.log(secret.username);
document.getElementById("secret").innerHTML = secret.password;
<p>
Your Password is : <spanid="secret"></span></p>

Post a Comment for "How To Extract Fields From This Aws Secretsmanager Json Object?"