{"id":15507,"date":"2023-11-22T19:35:37","date_gmt":"2023-11-22T14:05:37","guid":{"rendered":"https:\/\/codeforgeek.com\/?p=15507"},"modified":"2023-11-22T19:45:15","modified_gmt":"2023-11-22T14:15:15","slug":"json-string-to-object-nodejs","status":"publish","type":"post","link":"https:\/\/codeforgeek.com\/json-string-to-object-nodejs\/","title":{"rendered":"JSON.parse(): Convert JSON String To Object Using NodeJS"},"content":{"rendered":"\n<p>JSON stands for JavaScript Object Notation used to structure data based on JavaScript object syntax. It is small in size making it fast to transfer between server and client for transmission of information.<\/p>\n\n\n\n<p>JSON is based on JavaScript but due to its capability, it is implemented by many other programming languages like C, PHP, Python, etc.<\/p>\n\n\n\n<p>A JSON string is a string containing JSON that is needed to change in another form that can operate by JavaScript and since the objects are best suited for JavaScript, it is recommended to convert a JSON String to a JavaScript Object before performing any operations.<\/p>\n\n\n\n<p>There can be many ways to convert a JSON string to a JavaScript object using Node.js, but we will see the most reliable method for this which is the JSON.parse() method.<\/p>\n\n\n\n<p><strong><em>Also Read:<\/em><\/strong><a href=\"https:\/\/codeforgeek.com\/nodejs-convert-string-to-int\/\"><strong><em> NodeJS Convert String To Int \u2013 A Step-by-Step Guide<\/em><\/strong><\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Structure of JSON String<\/h2>\n\n\n\n<p>The structure of JSON is simpler and easy to read by humans. Below is a JSON string containing two keys, name and age, along with their value.\u00a0<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconst JSONString = `{\n    &quot;fullname&quot;: &quot;Stardew Valley&quot;, \n    &quot;age&quot;: 30\n}`;\n<\/pre><\/div>\n\n\n<p>Here we are using backticks (template literals) in place of quotation marks which are commonly used for string representation. We use backticks to avoid invalid JSON string errors that can occur when we define a string using single quotes or double quotes and do not escape the corresponding quotes inside that string properly. Additionally, the template allows multiline strings without literal line breaks.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Structure of JavaScript Object\u00a0<\/h2>\n\n\n\n<p>Below is a JavaScript object with the same key-value pairs as above.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconst javascriptObject = {\n    fullName: &#039;Stardew Valley&#039;,\n    age: 30\n}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Difference Between JSON String and JavaScript Object<\/h2>\n\n\n\n<p>In JSON string the keys and value are wrapped inside double quotes (&#8220;), whereas in JavaScript object it is not required to wrap the key and value inside double quotes instead you can write the key without having any type of quotation marks and wrap the value if it is a string inside single or double quotes.<\/p>\n\n\n\n<p>There is an exception that if the key contains special characters such as dashes (-), it must be wrapped inside single quotes (&#8216;) to make it a valid JavaScript object.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconst javascriptObject = {\n    fullName: &#039;Stardew Valley&#039;,\n    age: 30,\n    &#039;is-vegetarian&#039;: true,\n}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Convert JSON String to JavaScript Object Using JSON.parse()<\/h2>\n\n\n\n<p>There is a method <strong>JSON.parse()<\/strong> that can convert a JSON string to an object literals in JavaScript. This method takes a JSON string as an argument, converts the string into an object, and returns it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax of JSON.parse()<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nJSON.parse(JSONString&#x5B;, optionalFunction]);\n<\/pre><\/div>\n\n\n<p><strong>Parameters:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>JSONString<\/strong> is a JSON string to be converted into a JSON object,<\/li>\n\n\n\n<li><strong>optionalFunction<\/strong> is an optional parameter. It<strong> <\/strong>is a callback function that is implemented on each pair of objects. This can be used to change or update values or filter results during parsing.<\/li>\n<\/ul>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<p>We have a JSON string containing multiple values and arrays.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconst JSONString = `{\n    &quot;fullname&quot;: &quot;Stardew Valley&quot;, \n    &quot;age&quot;: 30, \n    &quot;is-vegetarian&quot;: true,\n    &quot;hobbies&quot;: &#x5B;&quot;farming&quot;, &quot;fishing&quot;, &quot;mining&quot;, &quot;foraging&quot;, &quot;fighting&quot;],\n    &quot;address&quot;: {&quot;street&quot;: &quot;123 Main St&quot;, &quot;city&quot;: &quot;Pelican Town&quot;, &quot;state&quot;: &quot;California&quot;, &quot;zip&quot;: &quot;12345&quot;},\n    &quot;phone-numbers&quot;: &#x5B;{&quot;type&quot;: &quot;home&quot;, &quot;number&quot;: &quot;123-456-7890&quot;}, {&quot;type&quot;: &quot;work&quot;, &quot;number&quot;: &quot;123-456-7890&quot;}]   \n}`;\n<\/pre><\/div>\n\n\n<p>Pass this as an argument to <strong>JSON.parse() <\/strong>method to convert it into a JavaScript Object.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconst javascriptObject = JSON.parse(JSONString);\n<\/pre><\/div>\n\n\n<p>Print the returned Object in the console.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconsole.log(javascriptObject);\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Complete Code to Convert JSON to JavaScript Object<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconst JSONString = `{\n    &quot;fullname&quot;: &quot;Stardew Valley&quot;, \n    &quot;age&quot;: 30, \n    &quot;is-vegetarian&quot;: true,\n    &quot;hobbies&quot;: &#x5B;&quot;farming&quot;, &quot;fishing&quot;, &quot;mining&quot;, &quot;foraging&quot;, &quot;fighting&quot;],\n    &quot;address&quot;: {&quot;street&quot;: &quot;123 Main St&quot;, &quot;city&quot;: &quot;Pelican Town&quot;, &quot;state&quot;: &quot;California&quot;, &quot;zip&quot;: &quot;12345&quot;},\n    &quot;phone-numbers&quot;: &#x5B;{&quot;type&quot;: &quot;home&quot;, &quot;number&quot;: &quot;123-456-7890&quot;}, {&quot;type&quot;: &quot;work&quot;, &quot;number&quot;: &quot;123-456-7890&quot;}]   \n}`;\n\nconst javascriptObject = JSON.parse(JSONString);\n\nconsole.log(javascriptObject);\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n{\n  fullname: &#039;Stardew Valley&#039;,\n  age: 30,\n  &#039;is-vegetarian&#039;: true,\n  hobbies: &#x5B; &#039;farming&#039;, &#039;fishing&#039;, &#039;mining&#039;, &#039;foraging&#039;, &#039;fighting&#039; ],\n  address: {\n    street: &#039;123 Main St&#039;,\n    city: &#039;Pelican Town&#039;,\n    state: &#039;California&#039;,\n    zip: &#039;12345&#039;\n  },\n  &#039;phone-numbers&#039;: &#x5B;\n    { type: &#039;home&#039;, number: &#039;123-456-7890&#039; },\n    { type: &#039;work&#039;, number: &#039;123-456-7890&#039; }\n  ]\n}\n<\/pre><\/div>\n\n\n<p>Here you can see that the return value is an object meaning we have successfully converted JSON string into a JavaScript object.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Handling Error\u00a0<\/h3>\n\n\n\n<p>It may be possible that the string you are trying to convert is not a valid JSON string and hence it may cause unexpected behaviour of the code making error handling necessary, let&#8217;s see how.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconst JSONString = &#039;Hello World!&#039;;\n\ntry {\n  const javascriptObject = JSON.parse(JSONString);\n  console.log(javascriptObject);  \n} catch (error) {\n  console.log(&quot;Please insert a valid JSON string&quot;);\n}\n<\/pre><\/div>\n\n\n<p>Here the given string is not a valid JSON string so the code inside the catch block will be executed. You can take more control over the program by writing your own code in catch block.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nPlease insert a valid JSON string\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Convert JavaScript Object To JSON String Using JSON.stringify()<\/h2>\n\n\n\n<p>In case you want, you can convert the JavaScript object back to a JSON-formatted string using another special method, JSON.stringify().<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconst javascriptObject = {\n  fullname: &#039;Stardew Valley&#039;,\n  age: 30,\n  &#039;is-vegetarian&#039;: true,\n  hobbies: &#x5B;&#039;farming&#039;, &#039;fishing&#039;, &#039;mining&#039;, &#039;foraging&#039;, &#039;fighting&#039;],\n  address: {\n    street: &#039;123 Main St&#039;,\n    city: &#039;Pelican Town&#039;,\n    state: &#039;California&#039;,\n    zip: &#039;12345&#039;\n  },\n  &#039;phone-numbers&#039;: &#x5B;\n    { type: &#039;home&#039;, number: &#039;123-456-7890&#039; },\n    { type: &#039;work&#039;, number: &#039;123-456-7890&#039; }\n  ]\n};\n\nconst JSONString = JSON.stringify(javascriptObject);\nconsole.log(JSONString);\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n{\n  &quot;fullname&quot;: &quot;Stardew Valley&quot;,\n  &quot;age&quot;: 30,\n  &quot;is-vegetarian&quot;: true,\n  &quot;hobbies&quot;: &#x5B;&quot;farming&quot;, &quot;fishing&quot;, &quot;mining&quot;, &quot;foraging&quot;, &quot;fighting&quot;],\n  &quot;address&quot;: {\n    &quot;street&quot;: &quot;123 Main St&quot;,\n    &quot;city&quot;: &quot;Pelican Town&quot;,\n    &quot;state&quot;: &quot;California&quot;,\n    &quot;zip&quot;: &quot;12345&quot;\n  },\n  &quot;phone-numbers&quot;: &#x5B;\n    {&quot;type&quot;: &quot;home&quot;, &quot;number&quot;: &quot;123-456-7890&quot;},\n    {&quot;type&quot;: &quot;work&quot;, &quot;number&quot;: &quot;123-456-7890&quot;}\n  ]\n}\n<\/pre><\/div>\n\n\n<p>We got the same result from where we started.<\/p>\n\n\n\n<p><strong>Read More: <a href=\"https:\/\/codeforgeek.com\/express-send-json-response\/\" data-type=\"link\" data-id=\"https:\/\/codeforgeek.com\/express-send-json-response\/\">Send a JSON response using Express Framework<\/a><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>JavaScript uses objects everywhere, and performing operations on it is easier than other forms of data structures. A JSON is also based on JS object structure, it is smaller in size and easily understandable. It is used to store data and transmission data from server to client.&nbsp;<\/p>\n\n\n\n<p>JSON is used by many other programming languages which can parse it into a format that they can understand. Since JavaScript performs well with objects, it is recommended to convert a string into a JavaScript object in Node.js before performing any further operations. This can be easily done using a method <strong>JSON.parse() <\/strong>that takes a JSON string as an argument and returns a JavaScript object. Hope this tutorial helps you convert string to object in JavaScript.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Reference<\/strong><\/h2>\n\n\n\n<p><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/JSON\/parse\" target=\"_blank\" rel=\"noopener\">https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/JSON\/parse<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>JSON stands for JavaScript Object Notation used to structure data based on JavaScript object syntax. It is small in size making it fast to transfer between server and client for transmission of information. JSON is based on JavaScript but due to its capability, it is implemented by many other programming languages like C, PHP, Python, [&hellip;]<\/p>\n","protected":false},"author":79,"featured_media":23918,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_surecart_dashboard_logo_width":"180px","_surecart_dashboard_show_logo":true,"_surecart_dashboard_navigation_orders":true,"_surecart_dashboard_navigation_invoices":true,"_surecart_dashboard_navigation_subscriptions":true,"_surecart_dashboard_navigation_downloads":true,"_surecart_dashboard_navigation_billing":true,"_surecart_dashboard_navigation_account":true,"_uag_custom_page_level_css":"","footnotes":""},"categories":[14],"tags":[],"class_list":["post-15507","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-nodejs"],"blocksy_meta":[],"uagb_featured_image_src":{"full":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2022\/12\/Convert-String-To-Object.png",1000,600,false],"thumbnail":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2022\/12\/Convert-String-To-Object-150x150.png",150,150,true],"medium":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2022\/12\/Convert-String-To-Object-300x180.png",300,180,true],"medium_large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2022\/12\/Convert-String-To-Object-768x461.png",768,461,true],"large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2022\/12\/Convert-String-To-Object.png",1000,600,false],"1536x1536":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2022\/12\/Convert-String-To-Object.png",1000,600,false],"2048x2048":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2022\/12\/Convert-String-To-Object.png",1000,600,false]},"uagb_author_info":{"display_name":"Aditya Gupta","author_link":"https:\/\/codeforgeek.com\/author\/aditya\/"},"uagb_comment_info":0,"uagb_excerpt":"JSON stands for JavaScript Object Notation used to structure data based on JavaScript object syntax. It is small in size making it fast to transfer between server and client for transmission of information. JSON is based on JavaScript but due to its capability, it is implemented by many other programming languages like C, PHP, Python,&hellip;","_links":{"self":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/15507","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/users\/79"}],"replies":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/comments?post=15507"}],"version-history":[{"count":0,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/15507\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media\/23918"}],"wp:attachment":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media?parent=15507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/categories?post=15507"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/tags?post=15507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}