Using a fetch request to get some data. The JSON object path depends on a string variable. How would I convert the string into an object that can be referenced as an array in the path. Here's my code:
var path = 'bob';
fetch(request)
.then(function(response) {
if (!response.ok) {
throw Error(response.statusText);
}
return response.json();
}).then(function(json) {
var data = json.message.path[0].text;
}).catch(function(error) {
console.log(error);
});
JSON:
{
"message": {
"bob": [
{
"name_id": "3351",
"name": "bob",
"text": "lorem ipsum"
},
{
"name_id": "34562",
"name": "bob",
"text": "lorem ipsum2"
}
]
}
Basically, path
defines the correct object to be used in the dot notation. BUT - the object also has to be able to reference the array.
Any solutions?