Navigate back to the homepage

How to remove backslash from json object in javascript?

Infinitbility
JavaScript
January 18th, 2022 · 1 min read
keep affilitate

Hello Friends 👋,

Welcome to Infinitbility.

When we stringify JSON multiple times it will add slashes in JSON and that’s why many folks getting these errors.

check the following example.

1let users = [
2 { id: 1, name: 'Infinitbility', email: '[email protected]' },
3 { id: 2, name: 'notebility', email: '[email protected]' },
4 { id: 3, name: 'stackbility', email: '[email protected]' },
5];
6
7console.log(JSON.stringify(JSON.stringify(users)));
8
9console.log(JSON.stringify(JSON.stringify(JSON.stringify(users))));

Here, I have stringify double and triple times to check their output, and here is the result.

Javascript multiple JSON stringify example
Javascript multiple JSON stringify example

Now, we have to same times parse the string to get actual JSON like the following example.

1let users = [
2 { id: 1, name: 'Infinitbility', email: '[email protected]' },
3 { id: 2, name: 'notebility', email: '[email protected]' },
4 { id: 3, name: 'stackbility', email: '[email protected]' },
5];
6
7let userStr = JSON.stringify(JSON.stringify(JSON.stringify(users)));
8
9console.log(userStr);
10
11console.log(JSON.parse(JSON.parse(JSON.parse(userStr))));

Output

Javascript multiple JSON parse example
Javascript multiple JSON parse example

but when you are an exception, and above solution not working for you.

Replace backslash to nothing in global level

Replace your backslash of every occurrence using the /\\/g regex and javascript replace method.

After your replace try to parse your string like the following example

1let str = "{\n \"taskNames\" : [\n \"01 Jan\", \n \"02 Jan\",\n \"03 Jan\",\n \"04 Jan\", \n \"05 Jan\"\n]}"
2var finial = str.replace(/\\/g, "");
3
4console.log(JSON.parse(finial));

Output

Javascript replace backslash example
Javascript replace backslash example

Thanks for reading…

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

melio affilitate

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

How to remove an object from an array in javascript

Javascript, Delete object from array example.

January 17th, 2022 · 1 min read

How to check array contains value in react-native?

React Native, array contains value example.

January 16th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]