I have this code:
function my_json_function(my_variable) {
var json_var = {
my_variable: [{
"firstName": "John",
"lastName": "Doe"
}, {
"firstName": "Anna",
"lastName": "Smith"
}]
};
return json_var;
}
So, the problem is:
I pass to the function a variable named my_variable, and I want to assign it to the name of the json group.
So, I explain what I mean:
For example if my_variable = "employees", the function have to produce the following result:
"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"}
]
I've tried to casting like this: String(my_variable), but it return me the error:
Uncaught SyntaxError: Unexpected token :
where am I wrong? thanks