Is possible to get the value from json object using the path as variable?

Hi,

Is possible to get the value from json object given I have a variable that contains the path for example:

let objC = {
page: {
images: {
logo: ‘randomValue’,
footer: ‘randomValue2’
}
}
};

let pathJson = ‘page.images.logo’;

Remember to include the object name…

objC.page.images.logo

Hello, and welcome to the forums!

If you really want to retrieve a value from an object using a string containing the path, you could use the Lodash get() method, or you could write your own if you didn’t want to use a 3rd party library.

Here’s an article that outlines the usage of Lodash’s get() method, but also talks about recreating the same functionality in vanilla JS.

1 Like