0

I am trying to create a javascript object,

var systemName = {"system" : varA};

But I want the object to be in the form `{"system" :"varA"}

with varA having the variable value but inserted inside double quotes. I have tried {"system" : "'+ varA +'"}; but that did not do the trick. Can you point out what I am doing wrong here? I know its a simple things. But sometimes these small things get us stuck at certain points

5
  • 1
    If it is a string you don't need that Commented Apr 25, 2014 at 14:16
  • I don't get what you want. What's wrong with {"system" :"varA"}? Commented Apr 25, 2014 at 14:16
  • is varA is variable or string ? Commented Apr 25, 2014 at 14:17
  • Dupe of stackoverflow.com/questions/417645/… ? Commented Apr 25, 2014 at 14:17
  • possible duplicate of Convert JS object to JSON string Commented Apr 25, 2014 at 14:54

3 Answers 3

1

Try this instead

var systemName = {};
systemName.system = varA;

(or)

systemName["system"] = varA;
Sign up to request clarification or add additional context in comments.

Comments

1

You don't want to do this. You shouldn't do this. If it is a string, the JSON parser will handle it for you. Don't worry about adding quotes to it. There is no reason for you to put quotes around the literal value of the variable. You can put quotes around it at the time of output, if you need to to.

var varA = "Hello";
var systemName = {"system" : varA};

console.log(JSON.stringify(systemName));
// {"system":"Hello"} 

http://jsfiddle.net/FWBub/

But, if you must do so:

var varA = '"Hello"';
var systemName = {"system" : varA};

console.log(JSON.stringify(systemName));
{"system":"\"Hello\""} 

http://jsfiddle.net/FWBub/1

Comments

0

JSON.stringify(varA) will add JSON quotes around the value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.