0

I'm trying to create a JSON object in Javascript using a variable key. The object specifically is a set of AWS credentials, but I don't think knowledge of AWS is required to answer my question. I have a function that looks like this:

var AWS = require('aws-sdk');
var config = require('./config.json');
var token = 'my_token';
var key = config.DEVELOPER_PROVIDER_NAME + '/' + config.USER_POOL_ID;
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
    IdentityPoolId : config.IDENTITY_POOL_ID,
    Logins : {
        key : token
    }
});

The idea is that AWS.CognitoIdentityCredentials needs to be instantiated with the IdentityPoolId and Logins elements, but the Logins element is an object which requires a key which changes based on the values in the config file. The current syntax would create the Logins object with an element literally named "key". I need the key to be named based on the value of my variable key.

John Riley
  • 468
  • 1
  • 6
  • 23
  • Please educate yourself on what "JSON" means. (Hint: it's a string-based format for serializing data, often for use in sending data back and forth from servers. It has nothing to do--OK, very little to do with--JavaScript objects, which is what you are concerned with.) –  Apr 26 '17 at 14:01

1 Answers1

2

You can create your object manually before initializing the AWS.CognitoIdentityCredentials

var AWS = require('aws-sdk');
var config = require('./config.json');
var token = 'my_token';
var key = config.DEVELOPER_PROVIDER_NAME + '/' + config.USER_POOL_ID;
var obj = {
    IdentityPoolId : config.IDENTITY_POOL_ID,
    Logins: {}
};
obj.Logins[key] = token;
AWS.config.credentials = new AWS.CognitoIdentityCredentials(obj);
Weedoze
  • 13,683
  • 1
  • 33
  • 63