0

I´m receiving a message that I have to parse to create a JSON object from a .NET application in javascript, but when I parse the message using the following instructions:

var messagePropertiesString  = JSON.stringify(messageObject.json);
var messageProperties = JSON.parse(messagePropertiesString);

The results contains a backslash, so I´m not able to convert it to a JSon object since it has the backslash.

{\"TravelNumber\":1,\"Unit\":\"g\",\"Weight\":0}

How can I remove the extra backslash?

6
  • messageProperties = messageProperties.replace('\\', ''); ? Commented Oct 21, 2016 at 4:21
  • with a RegExp : messagePropertiesString.replace(/\\/g,'') Commented Oct 21, 2016 at 4:22
  • But, for me, the problem is on the .NET application side ;) Commented Oct 21, 2016 at 4:26
  • 2
    That looks like the output of JSON.stringify. JSON.parse should decode the escapes. What is the original message? Commented Oct 21, 2016 at 4:27
  • Yes, probably is the way the serializer do its job, but that code is not at my side. I fixes using Lcf.vs recomendation. Commented Oct 21, 2016 at 4:33

1 Answer 1

3

messageObject.json is already a string, so there is no need to stringify it, and when you do you get a string that would need to be parsed twice (the first parse will just undo the stringify and get you back a string of JSON) to get an object:

var messagePropertiesString  = JSON.stringify( messageObject.json );
var messageProperties = JSON.parse( JSON.parse( messagePropertiesString ) );

Instead, you should skip the stringify, and just parse it once:

var messageProperties = JSON.parse( messageObject.json );
Sign up to request clarification or add additional context in comments.

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.