-2

var skillTargetID = "1234";
var agentId = "2345";
var firstName = "Ganesh";
var lastName = "Putta";
var userName = "ganesh@gmail.com"

agentjson = {
  "refURL": "/unifiedconfig/config/agent/" + skillTargetID + "," + "agentId" + ":" + agentId + "," + "firstName" + ":" + firstName + "," + "lastName" + ":" + lastName + "," + "userName" + ":" + userName
};

console.log(agentjson);

I got a string like below

"refURL":"/unifiedconfig/config/agent/5113","agentId":"1312","firstName":"John","lastName":"Elway","userName":"jelway"

i need to concatenate above string using javascript with dynamic values, i tried, but in the result double quotes are not appending for every word.

agentjson={"refURL":"/unifiedconfig/config/agent/"+skillTargetID+","+"agentId"+":"+agentId+","+"firstName"+":"+firstName+","+"lastName"+":"+lastName+","+"userName"+":"+userName};

this is result i got.

{"refURL": "/unifiedconfig/config/agent/1234,agentId:2345,firstName:Ganesh,lastName:Putta,userName:ganesh@gmail.com"}

how to cancatenate string to get the exact result like first one.

Ganesh Putta
  • 2,622
  • 2
  • 19
  • 26
  • Please create a [mcve] preferably with a [runnable stack snippet](https://meta.stackoverflow.com/questions/358992). It's not clear whether the first snippet is an object literal or the entire thing is a string – adiga Jan 31 '23 at 14:26
  • @adiga thanks for letting me know. i have added the snippet now – Ganesh Putta Jan 31 '23 at 14:31
  • define another variable: `var refURL = "/unifiedconfig/config/agent/" + skillTargetID` and then create an object using [shorthand property names](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#property_definitions) syntax. And then create a JSON string of the object: `JSON.stringify({ refURL, agentId, firstName, lastName, userName })` – adiga Jan 31 '23 at 14:36
  • 1
    Of the *literally* hundreds (edit: no *[thousands](https://www.google.com/search?q=site%3Astackoverflow.com+concatenate+quote+javascript)*) of duplicate questions already on SO that ask this exact question, this one has a good selection of solutions: https://stackoverflow.com/questions/9714759/put-quotes-around-a-variable-string-in-javascript In *your case* as it's JSON, stringify an object makes the most sense (as provided in the answer below, so not closed as a duplicate). – freedomn-m Jan 31 '23 at 14:51

1 Answers1

2

You need an object and then you need to stringify it

You can use template literals to embed variables in a string and the rest you can just write the variable name in the object

const createJSONString = (skillTargetID, agentId, firstName, lastName, userName) => {
  const obj = {refURL : `/unifiedconfig/config/agent/${skillTargetID}`,
  agentId, firstName, lastName, userName};
  return JSON.stringify(obj);
}


var skillTargetID = "1234";
var agentId = "2345";
var firstName = "Ganesh";
var lastName = "Putta";
var userName = "ganesh@gmail.com"

console.log(createJSONString(skillTargetID, agentId, firstName, lastName, userName))
mplungjan
  • 169,008
  • 28
  • 173
  • 236