javascript string to json – 3 Ways to Convert JS Object to JSON String

javascript string to json Use the JSON.stringify() Method. You can use the simple javascript JSON.stringify() method to simply convert a JavaScript object a JSON string.

javascript string to json

You can use the JSON.parse() method in JavaScript, to convert a JSON string into a JSON object.

Convert String to JSON Using json.stringify()

Using json.stringify()

JSON.stringify(source_of_data)

<script>
const text = '[ "virat", "rohit", "sachin", "dhoni" ]';
const players = JSON.parse(text);
document.getElementById("players_list").innerHTML = players[0];
</script>

Converting a string to JSON object

var players = JSON.parse(string);

Convert JS object to JSON string

var players = {
  "name": "pakainfo_com"
};
console.log(JSON.stringify(players));

Parsing JSON Data in JavaScript

Example

var players = '{"name": "virat", "rank": 986, "sirname": "Kohali"}';

var obj = JSON.parse(players);

alert(obj.name); // Results: virat
alert(obj.rank); // Results: 986
alert(obj.sirname); // Results: Kohali

Stringify a JavaScript Object
Converting JS object to JSON string

var obj = {"name": "virat", "rank": 986, "sirname": "Kohali"};

var players = JSON.stringify(obj);
alert(players);

Stringify a JavaScript Array
Converting JS array to JSON string

var players = ["virat", "sachin", "yuvraj", "rohit", "dhoni"];

var json = JSON.stringify(players);
alert(json);

How to Convert JSON String to JSON Object in JavaScript?

index.html

<!DOCTYPE html>
<html>
<head>
    <title>Convert JSON String to JSON Object in JavaScript - www.pakainfo.com</title>
</head>
<body>
    <pre id="jsonData"
        style="border:solid 1px #CCC;padding:0 5px;">
			[
			    {
			        "ID": "001",
			        "Name": "virat Kohali",
			        "Type": "Batsmen",
			        "player Team Name": "India"
			    },
			  
				{
			        "ID": "002",
			        "Name": "Sachin Tendlukar",
			        "Type": "All",
			        "player Team Name": "Gujrat" 
			    },

				{
			        "ID": "003",
			        "Name": "krunal Pandya",
			        "Type": "Bolwer",
			        "player Team Name": "IPL"  
			    }
			]
    </pre>

    <p>
        <input type="button" id="btClick" value="String to JSON" onclick="str2json()" />
    </p>
</body>
<script>
    function str2json() {
        var players = document.getElementById('jsonData').innerHTML;
        var playersjsonObj = JSON.parse(players);
        console.log(playersjsonObj);
    }
</script>
</html>

Resluts

[
    {
        "ID": "001",
        "Name": "virat Kohali",
        "Type": "Batsmen",
        "player Team Name": "India"
    },
  
	{
        "ID": "002",
        "Name": "Sachin Tendlukar",
        "Type": "All",
        "player Team Name": "Gujrat" 
    },

	{
        "ID": "003",
        "Name": "krunal Pandya",
        "Type": "Bolwer",
        "player Team Name": "IPL"  
    }
]

Don’t Miss : PHP JSON stringify

Also Read This ๐Ÿ‘‰   how to fetch data from database in php mysql using search button?

I hope you get an idea about javascript string to json.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, donโ€™t forget to share.