I have just started learning about JSON and am currently working on a JavaScript program. I've been searching for a straightforward solution, but I may not be framing my question correctly.
My goal is to allow users to input their information which will then be added to a JSON object. However, I'm struggling to get JSON to accept the value. Let's say we have userName="Bob"
.
const userJSON=`{
"playerName": userName
}`;
I want userJSON
to result in "playerName": "Bob"
. But unfortunately, I keep getting an error stating that userName
is not valid JSON.
Is there a simpler way to achieve this? I can successfully parse information if it appears as "playerName":"Bob"
, but since this is intended for a game where users can add their own usernames, I need it to be more dynamic.