How to Remove JSON Object Whitespace in Javascript?

Nov 10, 2022 . Admin



Hello friends,

In this quick example, let's see how to remove JSON object whitespace in javascript. Here you will learn to remove object whitespace with an example. This tutorial will give you a simple example of removing JSON object whitespace in javascript, this example will help you remove object whitespaces of JSON.

To remove the whitespaces spaces, newline, or tab present in the key use replace() and replace a single space " " between the word of the key instead of many whitespaces.

So, let's see bellow solution:

Example:
index.html
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>How to Remove JSON Object Whitespace in Javascript?</title>
</head>
<body>
<script type="text/javascript">

    //create jsonPlayer
    var jsonPlayer = "  Rishabh   Pant,    Rohit   Sharma,     Virat    Kohli  ";

    //use replace() to remove whitespaces
    var removeSpace = jsonPlayer.replace(/\s\s+/g, " ");
    
    //print remove whitespaces
    console.log(removeSpace);

</script>
</body>
</html>
Check The Console For Output:
Rishabh Pant, Rohit Sharma, Virat Kohli 

I hope it will help you...

#Javascript