How to read the values of nested objects from the Config file

213 Views Asked by At

This is my config.json File

   {
     "timeout":10000
     
     "Status":{
         "Error":{"message":"Runtime","continue":False}
     }
   }

First I want to read the value "Error" and the the value "False" and assign them to two different variables. How can I read those two values from the config.json file.

Any help is appreciated. Thankyou.

1

There are 1 best solutions below

0
On

To make this easy, make it a .ts file instead of a .json file:

app.config.ts:

export const appConfig = {
  Status:{
    Error:{message:"Runtime",continue:False}
  }
}

Then just import appConfig where you need to access it:

import { appConfig } from '../../app.config' // example, may be a different path

and access it as follows:

console.log(appConfig.Status.Error.continue)

Or:

console.log(Object.keys(appConfig.Status)[0])

PS it is possible to import a JSON file if that is necessary but importing JSON is less powerful since you cannot add code to a JSON file but you can to a .ts file