1

i have a simple json post request. i have nested json object to send , the key "text" contains 2 subkeys like this : "text" : {"nom":"user1","browser":"chrome"}

the 2 subkeys ( "name","browser") there values are javascript params, i want to replace them in the json object to send . i tried this!

var var1=session.get("userConnected");
var var2=checkBrowser();
var values =
{ type: "text"
, text:"{\"name\":\"+var1+\",\"browser\":" +var2+"}" 
};

Please how do i replace javascript params inside json object? and thanks alot

2 Answers 2

2

Use JSON.stringify it will make life a lot easier.

var var1 = "John Doe"; // session.get("userConnected");
var var2 = "chrome"; //checkBrowser();
var values = {
  type: "text",
  text: JSON.stringify({
    name: var1,
    browser: var2
  })
};
console.log(values);
console.log(JSON.parse(values.text));

Sign up to request clarification or add additional context in comments.

Comments

-1

Yes. Decode the JSON object into an in-memory structure, change that structure, then Encode the resulting structure as JSON. Don't try to fiddle with the file as a string.

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.