Home > database >  Read Json keys of local file in environment.ts
Read Json keys of local file in environment.ts

Time:03-25

I am maintaining JSON file in src/assets/environments/env.json

{
  key1:somedata1,
  key2:somedata2
}

I want to access these keys from another typescript file (without http client or any external Library)

The file I am gonna call these keys is environment.ts which is located in src/environment/

CodePudding user response:

I think it's enough to just import the file.

import env from './assets/environments/env.json';

CodePudding user response:

Having this environment file as:

export const environment = {
  production: false,
  key1: 'somevalue',
  key2: 'someValue2'
};

Then you could grab the keys like this:

import { environment } from 'src/environments/environment';

ngOnInit() {
  const env = environment;
  const keys = Object.keys(env);
  
  console.log(keys); // ['production', 'key1', 'key2']
}
  • Related