I'm trying to extract data from a JSON file without knowing the exact location of the data. Here is an example JSON:
var names= [
{
"category":"category1" ,
"name1":"david",
"name2":"jhon",
"name3":"peter"
},
{
"category":"category2" ,
"name1":"Smith" ,
"name2":"Anna",
}
]
Let's say I have a string variable:
var str='category2';
How can I access the value of category2.name1 using the variable? I want to avoid hardcoding names[1].name1 and also steering clear of using a for loop.