1

I got this value and I need to update the property application_name to myApp,

i've tried with JSON.parse but I got error "unexpected token ' "

this is a short example of the "string" which I got... Any idea?

"'{\"instance_id\":\"71658c8c-8fcf-546bb7b7cbdc\",\"application_name\":\"ht10\"}'\r"
1
  • remove the trailing \r and then parse. Commented Aug 12, 2015 at 19:17

2 Answers 2

3

https://jsfiddle.net/rLgsb3p7/

{\"instance_id\":\"71658c8c-8fcf-546bb7b7cbdc\",\"application_name\":\"ht10\"}

Is a json string try:

var v = "'{\"instance_id\":\"71658c8c-8fcf-546bb7b7cbdc\",\"application_name\":\"ht10\"}'\r";
v = v.slice(1,v.length-2);
var x = JSON.parse(v);
console.log(x);

/r counts as a single character

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

4 Comments

Thanks a lot 1+ , last question assume I need to add(at the end) the quotes what is the opposite to v = v.slice(1,v.length-2);? and what do you mean by /r counts as a single character?
That is a bit beyond the scope of your initial query. just know \r is treated as a single character. [ H ] [ E ] [ L ] [ L ] [ O ] [ \r ]
if you need to append a string just use v += "appeneded text";
if you want to re-add your quotes use v = "'" + v + "'";
2

Your string has an extra set of quotes which is causing JSON.parse to fail. This parses:

JSON.parse("{\"instance_id\":\"71658c8c-8fcf-546bb7b7cbdc\",\"application_name\":\"ht10\"}")

1 Comment

Thanks but I got it as parameter to function so what should i do in this case?

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.