{"id":2957,"date":"2022-09-11T08:41:57","date_gmt":"2022-09-11T08:41:57","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=2957"},"modified":"2023-02-22T13:26:20","modified_gmt":"2023-02-22T13:26:20","slug":"json-stringify","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/json-stringify\/","title":{"rendered":"JSON.Stringify"},"content":{"rendered":"\n<p class=\"has-text-align-center\"><strong>JSON.Stringify ():<\/strong><\/p>\n\n\n\n<p>We are now familiar with the thing, that we use JSON for data transfer from\/to a web server. The thing is that when we are sending data to the web server, the data needs to be a string. So, if you want to send some JavaScript object to the server, you would need to first convert it into a string, and this is what stringify method does for us.<\/p>\n\n\n\n<p>Using the stringify method, we can convert the JavaScript object into a string. If you are still confused, do not worry, since we are going to look at an example, with which, many things would get clearer.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"JSON Stringify | Lecture 12 | JSON Course for Beginners\" width=\"696\" height=\"392\" src=\"https:\/\/www.youtube.com\/embed\/a7EQSE9dRxs?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>So, we are using the stringify method to convert our JavaScript object to a string. The stringify method also works on arrays. Now, let&#8217;s have a look at the example, through which, we can understand the stringify method.<\/p>\n\n\n\n<p><strong>&lt;!DOCTYPE html&gt;<\/strong><br><strong>&lt;html lang=&#8221;en&#8221;&gt;<\/strong><br><strong>&lt;head&gt;<\/strong><br><strong>&lt;meta charset=&#8221;UTF-8&#8243;&gt;<\/strong><br><strong>&lt;meta http-equiv=&#8221;X-UA-Compatible&#8221; content=&#8221;IE=edge&#8221;&gt;<\/strong><br><strong>&lt;meta name=&#8221;viewport&#8221; content=&#8221;width=device-width, initial-scale=1.0&#8243;&gt;<\/strong><br><strong>&lt;title&gt;JSON stringify&lt;\/title&gt;<\/strong><br><strong>&lt;\/head&gt;<\/strong><br><strong>&lt;body&gt;<\/strong><br><strong>&lt;script&gt;<\/strong><br><strong>var userData = {<\/strong><br><strong>userName:&#8221;GyaniPandit&#8221;, <\/strong><br><strong>followers:1000000, <\/strong><br><strong>posts:15000, <\/strong><br><strong>isVerified:true<\/strong><br><strong>}<\/strong><br><strong>var userDataString = JSON.stringify(userData);<\/strong><br><strong>console.log(userDataString);<\/strong><br><strong>&lt;\/script&gt;<\/strong><br><strong>&lt;\/body&gt;<\/strong><br><strong>&lt;\/html&gt;<\/strong><\/p>\n\n\n\n<p>As you can see, the above is a very simple program, where we have some simple HTML code, and within the script, we are having JavaScript. First of all, we have a simple JavaScript object, which has some key-value pairs. After that, we are using the JSON.stringify method, and pass the userData object as an argument. In this case, this object would be converted to a string, which follows JSON notation. After that, we are getting the string into the console. Let&#8217;s have a look at the output.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"406\" height=\"169\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/09\/JSON.stringify.jpg\" alt=\"\" class=\"wp-image-2958\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/09\/JSON.stringify.jpg 406w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/09\/JSON.stringify-300x125.jpg 300w\" sizes=\"auto, (max-width: 406px) 100vw, 406px\" \/><\/figure>\n<\/div>\n\n\n<p>As you can see, we have the JavaScript object converted to the JSON string. As you can see, this string follows the JSON notation.<\/p>\n\n\n\n<p>So, to summarize, the JSON.stringify method helps us to convert the JavaScript object to a string. So, whenever we are required to do so, we will make use of the JSON.stringify method.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JSON.Stringify (): We are now familiar with the thing, that we use JSON for data transfer from\/to a web server. The thing is that when we are sending data to the web server, the data needs to be a string. So, if you want to send some JavaScript object to the server, you would need [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[24,23],"class_list":{"0":"post-2957","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-json-tutorial","7":"tag-json","8":"tag-json-tutorial"},"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/2957","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=2957"}],"version-history":[{"count":3,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/2957\/revisions"}],"predecessor-version":[{"id":4950,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/2957\/revisions\/4950"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=2957"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=2957"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=2957"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}