Site Admin
Site Admin Founder of MeaningArticles
1236 Views

How to Convert Object to String and String to Object in Javascript?

Hello Dev.

In this Javascript article, i'm able to will let you realize the way to converts Javascript object into strings and string into json object.

The JSON.stringify() method is used to convert JSON object into JSON string. sometimes within the development, we need to serialize the data to strings in order to interact with APIs or web server.

The JSON.parse() method is used to convert JSON string into JSON object. sometimes you send json string to APIs or web server and at the web server side you want to convert that string to valid json object to read parameters. This conversion of an string to object can be without problems finished with the assist of the JSON.parse() method.

So let's start the lesson...

Below is an instance of converting object to string and string to object again:

<!DOCTYPE html>
<html>

<head>
    <title>How to Convert Object to String and String to Object in Javascript? - meaningarticles.com</title>
</head>

<body>
    <script>
    var obj = {
            url: "https:/meaningarticles.com/"
        };
      
        var stringObj = JSON.stringify(obj);
      
        console.log(typeof stringObj); // string
    
        var jsonObj=JSON.parse(stringObj)
    
        console.log(typeof jsonObj); // object
    </script>
</body>

</html>

In above example, i've defined a simple object and then I convert them into string the usage of JSON.stringify()  method. again i convert that json string into an object with the help of JSON.parse() method.

I hope it assists you, thanks for visiting my article if you like my article then share it with your friends on the social media platform.

Happy Coding.....