'get JSON property's value when path passed as string: property1.property2.property3 [duplicate]
Supposing I have a JSON object
let dictionary = require('../../es.json');
let obj = JSON.parse(dictionary);
and I have a string variable
let jsonPropertyPath = 'property1.property2.property3'
Now, I would like to get the value of the property property1.property2.property3 of an object obj. Is there any elegant way of doing this?
Solution 1:[1]
export const FindTranslation = (jsonPropertyPath: string): string => {
let dictionary = require('../locale/' + testContext.locale + '.json') as JSON;
return eval('dictionary.' + jsonPropertyPath);
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | KoBe |
