# How to parse an S3 JSON file into environment variables for Serverless.ts

**URL:** <https://forum.serverless.com/t/how-to-parse-an-s3-json-file-into-environment-variables-for-serverless-ts/12768>\
**Category:** Serverless Framework\
**Created:** [October 5, 2020, 9:22pm UTC](https://forum.serverless.com/t/how-to-parse-an-s3-json-file-into-environment-variables-for-serverless-ts/12768 "2020-10-05T21:22:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![urb4nc4rl](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/urb4nc4rl/32/4604_2.png) [@urb4nc4rl](https://forum.serverless.com/u/urb4nc4rl)\
**Post date:** [October 5, 2020, 9:22pm UTC](https://forum.serverless.com/t/how-to-parse-an-s3-json-file-into-environment-variables-for-serverless-ts/12768/1 "2020-10-05T21:22:47Z")

</div>

What is the syntax to parse an S3 .json file into individual environment variables in the Serverless.ts configuration?

I’ve tried the following without success to get the environment variable MY\_SECRET set to the value of MY\_SECRET in the bucket’s JSON.

```
functions: {
    hello: {
      environment: {
        MY_SECRET: "${s3:my-config/services/test/support-portal-api.json}"
      },

```

…this deploys but sets MY\_SECRET to the entire contents of the JSON file

```
functions: {
    hello: {
      environment: {
        MY_SECRET: JSON.parse("${s3:my-config/services/test/support-portal-api.json}").MY_SECRET
      },

```

…this fails to deploy with :

```
SyntaxError: Unexpected token $ in JSON at position 0
      at JSON.parse (<anonymous>)

```

What is the correct syntax? The documentation isn’t very clear. Please note this is a Typescript (Serverless.ts) file.

---

<div class="post-metadata">

**Author:** ![hndrsv](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/hndrsv/32/7597_2.png) [@hndrsv](https://forum.serverless.com/u/hndrsv)\
**Post date:** [August 10, 2023, 7:52am UTC](https://forum.serverless.com/t/how-to-parse-an-s3-json-file-into-environment-variables-for-serverless-ts/12768/2 "2023-08-10T07:52:44Z")

</div>

@urb4nc4rl Hello from the future, I need to do this. Did you figure out how to do it?

---

<div class="post-metadata">

**Author:** ![eyrockscript](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/eyrockscript/32/7730_2.png) [@eyrockscript](https://forum.serverless.com/u/eyrockscript)\
**Post date:** [November 15, 2023, 4:58pm UTC](https://forum.serverless.com/t/how-to-parse-an-s3-json-file-into-environment-variables-for-serverless-ts/12768/4 "2023-11-15T16:58:39Z")

</div>

Hi there!

I have solved this using require and ‘magic quotes’.

My project structure is:

```auto
My root folder:
| - config
| |-- dev.json
| |-- prd.json

```

I simply use `require` at the beginning of my serverless.ts file:

```auto
const env = require(`./config/${process.env.STAGE||'dev'}.json`);
console.log('env: ', env);

```

Then, I use it as a JSON object in the environment section:

```auto
    environment: {
      AWS_NODEJS_CONNECTION_REUSE_ENABLED: '1',
      NODE_OPTIONS: '--enable-source-maps --stack-trace-limit=1000',
      CLIENT_ID: env.CLIENT_ID,
      MNG_CLIENT_ID: env.MNG_CLIENT_ID,
      PRIVATE_KEY: env.PRIVATE_KEY,
      HOST: env.HOST,
      ORIGIN: env.ORIGIN,
      RPID: env.RPID,
      INCODE_URL: env.INCODE_URL,
      INCODE_API_KEY: env.INCODE_API_KEY,
      INCODE_FLOW_ID: env.INCODE_FLOW_ID,
    },

```
