Ozone
Ozone

Reputation: 429

Remove Backslashes from Json Data in JavaScript

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?

Upvotes: 40

Views: 174432

Answers (9)

Surya R Praveen
Surya R Praveen

Reputation: 3745

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 *

Upvotes: 0

Antonin GAVREL
Antonin GAVREL

Reputation: 11219

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

Upvotes: 0

Rishabh Jain
Rishabh Jain

Reputation: 49

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;
  }

Upvotes: 0

Bhargav Anand
Bhargav Anand

Reputation: 41

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.

Upvotes: 2

Jasir
Jasir

Reputation: 743

In React Native , This worked for me

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

Upvotes: 0

Abhinaba Nag
Abhinaba Nag

Reputation: 57

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);

Upvotes: 1

Felix Kling
Felix Kling

Reputation: 816404

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));

Upvotes: 55

Satish Sharma
Satish Sharma

Reputation: 9635

try this

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

Upvotes: 5

Edgar Villegas Alvarado
Edgar Villegas Alvarado

Reputation: 18344

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

Upvotes: 61

Related Questions