{"id":16416,"date":"2022-10-16T14:03:31","date_gmt":"2022-10-16T08:33:31","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-how-to-parse-a-json-object-from-ajax-call-in-java-script\/"},"modified":"2022-10-16T14:03:31","modified_gmt":"2022-10-16T08:33:31","slug":"solved-how-to-parse-a-json-object-from-ajax-call-in-java-script","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-how-to-parse-a-json-object-from-ajax-call-in-java-script\/","title":{"rendered":"[Solved] How to parse a JSON object from ajax call in Java Script"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-24048736\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"24048736\" data-parentid=\"24022341\" data-score=\"1\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>A couple of things:<\/p>\n<p>First, your Java servlet is not really returning a &#8220;string&#8221;. When you write <code>jsonArray.toString()<\/code>, yes, you are turning the array into a string, but that is solely for the purpose of writing it across the network. HTTP is a text protocol. So, what the <code>doGet<\/code> method is actually returning, in a sense, is a HTTP response (it just happens to be as text, it could very well be binary).<\/p>\n<p>With that, when a client (in this case, your JavaScript via <code>XMLHttpRequest<\/code>) makes a <code>GET<\/code> request to your server (your servlet), it is getting back the JSON response (yes, as text). Your <code>xmlhttp.responseText<\/code> variable in this case <em>should<\/em> contain the JSON you&#8217;ve shown in the question.<\/p>\n<p>Calling one of:<\/p>\n<ul>\n<li><code>JSON.parse(xmlhttp.responseText)<\/code><\/li>\n<li><code>jQuery.parseJSON(xmlhttp.responseText)<\/code><\/li>\n<li><code>$.parseJSON(xmlhttp.responseText)<\/code><\/li>\n<\/ul>\n<p>Should all return you the same object. With this object you can access its properties the way you want to. The following <em>should<\/em> work:<\/p>\n<pre><code>if (xmlhttp.readyState == 4 &amp;&amp; xmlhttp.status == 200) {\n    if (xmlhttp.responseText != null) {\n        var json = xmlhttp.responseText;\n        var arr = JSON.parse(json);\n        var data = arr[0].data;       \/\/ This is what you want to do?\n        var chart = arr[0].chart;     \/\/ This is what you want to do?\n        \/\/ try running alert(chart.xAxisName);\n        \/\/ and so on\n<\/code><\/pre>\n<p><strong>Side note<\/strong>: When you run <code>alert(obj);<\/code> where <code>obj<\/code> is a object, you are seeing <code>[object Object]<\/code> because that is how JavaScript represents objects as strings. If you want to see the internals of a JavaScript object, as others have pointed out, you are better off using <code>console.log(obj)<\/code>. (Also, upgrade or switch to a better browser. You will have access to better debugging tools.)<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">1<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved How to parse a JSON object from ajax call in Java Script <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] A couple of things: First, your Java servlet is not really returning a &#8220;string&#8221;. When you write jsonArray.toString(), yes, you are turning the array into a string, but that is solely for the purpose of writing it across the network. HTTP is a text protocol. So, what the doGet method is actually returning, in &#8230; <a title=\"[Solved] How to parse a JSON object from ajax call in Java Script\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-how-to-parse-a-json-object-from-ajax-call-in-java-script\/\" aria-label=\"More on [Solved] How to parse a JSON object from ajax call in Java Script\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[334,323,333,356],"class_list":["post-16416","post","type-post","status-publish","format-standard","hentry","category-solved","tag-ajax","tag-java","tag-javascript","tag-json"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] How to parse a JSON object from ajax call in Java Script - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-how-to-parse-a-json-object-from-ajax-call-in-java-script\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] How to parse a JSON object from ajax call in Java Script - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] A couple of things: First, your Java servlet is not really returning a &#8220;string&#8221;. When you write jsonArray.toString(), yes, you are turning the array into a string, but that is solely for the purpose of writing it across the network. HTTP is a text protocol. So, what the doGet method is actually returning, in ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-how-to-parse-a-json-object-from-ajax-call-in-java-script\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-16T08:33:31+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-parse-a-json-object-from-ajax-call-in-java-script\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-parse-a-json-object-from-ajax-call-in-java-script\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] How to parse a JSON object from ajax call in Java Script\",\"datePublished\":\"2022-10-16T08:33:31+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-parse-a-json-object-from-ajax-call-in-java-script\/\"},\"wordCount\":228,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"ajax\",\"java\",\"javascript\",\"json\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-parse-a-json-object-from-ajax-call-in-java-script\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-how-to-parse-a-json-object-from-ajax-call-in-java-script\/\",\"name\":\"[Solved] How to parse a JSON object from ajax call in Java Script - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-10-16T08:33:31+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-parse-a-json-object-from-ajax-call-in-java-script\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-how-to-parse-a-json-object-from-ajax-call-in-java-script\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-parse-a-json-object-from-ajax-call-in-java-script\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] How to parse a JSON object from ajax call in Java Script\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] How to parse a JSON object from ajax call in Java Script - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-how-to-parse-a-json-object-from-ajax-call-in-java-script\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] How to parse a JSON object from ajax call in Java Script - JassWeb","og_description":"[ad_1] A couple of things: First, your Java servlet is not really returning a &#8220;string&#8221;. When you write jsonArray.toString(), yes, you are turning the array into a string, but that is solely for the purpose of writing it across the network. HTTP is a text protocol. So, what the doGet method is actually returning, in ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-how-to-parse-a-json-object-from-ajax-call-in-java-script\/","og_site_name":"JassWeb","article_published_time":"2022-10-16T08:33:31+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-parse-a-json-object-from-ajax-call-in-java-script\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-parse-a-json-object-from-ajax-call-in-java-script\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] How to parse a JSON object from ajax call in Java Script","datePublished":"2022-10-16T08:33:31+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-parse-a-json-object-from-ajax-call-in-java-script\/"},"wordCount":228,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["ajax","java","javascript","json"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-parse-a-json-object-from-ajax-call-in-java-script\/","url":"https:\/\/jassweb.com\/solved\/solved-how-to-parse-a-json-object-from-ajax-call-in-java-script\/","name":"[Solved] How to parse a JSON object from ajax call in Java Script - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-10-16T08:33:31+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-parse-a-json-object-from-ajax-call-in-java-script\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-how-to-parse-a-json-object-from-ajax-call-in-java-script\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-parse-a-json-object-from-ajax-call-in-java-script\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] How to parse a JSON object from ajax call in Java Script"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/16416","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=16416"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/16416\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=16416"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=16416"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=16416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}