41

Remove Backslashes from JSON Data in JavaScript or jQuery

var str = "{"data":"{\n \"taskNames\" : [\n \"01 Jan\",\n \"02 Jan\",\n \"03 Jan\",\n \"04 Jan\",\n \"05 Jan\",\n \"06 Jan\",\n \"07 Jan\",\n \"08 Jan\",\n \"09 Jan\",\n \"10 Jan\",\n \"11 Jan\",\n \"12 Jan\",\n \"13 Jan\",\n \"14 Jan\",\n \"15 Jan\",\n \"16 Jan\",\n \"17 Jan\",\n \"18 Jan\",\n \"19 Jan\",\n \"20 Jan\",\n \"21 Jan\",\n \"22 Jan\",\n \"23 Jan\",\n \"24 Jan\",\n \"25 Jan\",\n \"26 Jan\",\n \"27 Jan\"]}

var finalData = str.replace("\\", "");

but this does not work for me. Any help?

7
  • 1
    It looks like your Json data is invalid. Commented Jan 10, 2014 at 5:14
  • That's a syntax error. Many of your " aren't escaped. Commented Jan 10, 2014 at 5:14
  • okay, I was unable to load whole data of Json, its too large i have did some of the data Commented Jan 10, 2014 at 5:15
  • You need to not have them in the first place. It is a server issue, not a client issue Commented Jan 10, 2014 at 5:16
  • okay, but i receive this data, can you please provide the solution for this. Commented Jan 10, 2014 at 5:17

9 Answers 9

63

Your string is invalid, but assuming it was valid, you'd have to do:

var finalData = str.replace(/\\/g, "");

When you want to replace all the occurences with .replace, the first parameter must be a regex, if you supply a string, only the first occurrence will be replaced, that's why your replace wouldn't work.

Cheers

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

6 Comments

when i used the above i'm getting this error TypeError: str.replace is not a function [Break On This Error] var finalData = taskNames.replace(/\\/g, "");
because in the code you're just providing there's no str, it's called taskNames
sorry my name convention was wrong I change but still getting same error Edgar
Great. The problem is that in js code you can't have line breaks in the middle of a string (in other languages, like php, you can). Here's the corrected solution: jsfiddle.net/edgarinvillegas/xPUT3/1
Hey Edgar Thanks its working..!!, Will implement this.. Thanks
|
56

tl;dr: You don't have to remove the slashes, you have nested JSON, and hence have to decode the JSON twice: DEMO (note I used double slashes in the example, because the JSON is inside a JS string literal).


I assume that your actual JSON looks like

{"data":"{\n \"taskNames\" : [\n \"01 Jan\",\n \"02 Jan\",\n \"03 Jan\",\n \"04 Jan\",\n \"05 Jan\",\n \"06 Jan\",\n \"07 Jan\",\n \"08 Jan\",\n \"09 Jan\",\n \"10 Jan\",\n \"11 Jan\",\n \"12 Jan\",\n \"13 Jan\",\n \"14 Jan\",\n \"15 Jan\",\n \"16 Jan\",\n \"17 Jan\",\n \"18 Jan\",\n \"19 Jan\",\n \"20 Jan\",\n \"21 Jan\",\n \"22 Jan\",\n \"23 Jan\",\n \"24 Jan\",\n \"25 Jan\",\n \"26 Jan\",\n \"27 Jan\"]}"}

I.e. you have a top level object with one key, data. The value of that key is a string containing JSON itself. This is usually because the server side code didn't properly create the JSON. That's why you see the \" inside the string. This lets the parser know that " is to be treated literally and doesn't terminate the string.

So you can either fix the server side code, so that you don't double encode the data, or you have to decode the JSON twice, e.g.

var data = JSON.parse(JSON.parse(json).data));

1 Comment

Thanks! I did this if (str.includes('\"')) { str = JSON.parse(str); }
5

try this

var finalData = str.replace(/\\/g, '');

1 Comment

when i used the above i'm getting this error TypeError: str.replace is not a function [Break On This Error] var finalData = str.replace(/\\/g, "");
2

Try to do JSON.parse(data), it should work. In most cases when the JSON is generated from Sitecore or any backend, they will come with all the slashes.

Comments

1

You need to deserialize the JSON once before returning it as response. Please refer below code. This works for me:

JavaScriptSerializer jss = new JavaScriptSerializer();
Object finalData = jss.DeserializeObject(str);

Comments

0

In React Native , This worked for me

name = "hi \n\ruser"
name.replace( /[\r\n]+/gm, ""); // hi user

Comments

0

After stumbling for hours, this worked out. You can use this function to convert your json string with backslashes to a javascript object.

  function strToObj(str: string) {
    var obj = {};
    if (str && typeof str === 'string') {
      var objStr = str.match(/\{(.)+\}/g);
      eval('obj =' + objStr);
    }
    return obj;
  }

Comments

0

JSON.parse(JSON.stringify(req.body.json)) did the work for me.

Comments

0

Method 1 : Using replace() method with a regular expression

// Input string
let origString = 'string / with some // slashes /';
 
// Display
console.log(origString);
 
// Replacement for slash
let replacementString = '*';
 
// Replaced string
let replacedString = origString.replace(/\//g, replacementString);
 
// Display output
console.log(replacedString);

Output

string / with some // slashes /
string * with some ** slashes *

Method 2: Using the JavaScript split() method

// Input String with slashes
let origString = 'string / with some // slashes /';

// Display input string
console.log(origString);

// Replacement for slash
let replacementString = '*';

// Replaced String
let replacedString = origString.split('/').join(replacementString);
            
// Display output
console.log(replacedString);

Output

string / with some // slashes /
string * with some ** slashes *

Method 3: Using JavaScript replaceAll() method

// Input string
let origString = 'string / with some // slashes /';
// Display input string
console.log(origString);

// replacement cahracter
let replacementString = '*';

// Replace all slash using replaceAll method;
let replacedString =
            origString.replaceAll('/', '*');

// Display output
console.log(replacedString);

Output

string / with some // slashes /
string * with some ** slashes *

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.