I have a working JavaScript code below which dynamically creates JSON object using JSON.parse method. Is there a shorter way to do this?
var a = '"hi"';
var obj = '{' + a + ':' + '"abc"' + '}';
console.log(JSON.parse(obj)); // outputs: OBJECT: {"hi":"abc"}
something simplier like…
var a='hi';
console.log({a:"abc"}); // but outputs: OBJECT: {a:"abc"}
You can use square brackets to access a property whose name you have as a string:
Note that JSON is just a data format. It is not JavaScript. You want a JavaScript object literal, not JSON.