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)

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




    Convert JSON String to JSON Object in JavaScript - www.pakainfo.com


    
			[
			    {
			        "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"  
			    }
			]
    



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

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.

Leave a Comment