Back to Top

How to parse the JSON string using jQuery

In this short and informative post, I am going to explain how to parse the JSON string using jQuery. JSON stands for a JavaScript Object Notation which is a data exchange format and human-readable data. Every object in JSON is stored with curly braces { } and an array is stored with brackets[ ].

Well, I am going to show two ways to read/parse JSON data with using the jQuery.

1. jQuery method for JSON DataType

In this method, You have to set the data type parameter as “json” in the AJAX request and the data will be directly parsed in your success callback and you use those data.

Let’s see the SYNTAX:

dataType: "json"

Now, let’s use this syntax into code:

 jQuery.ajax({
	  url: full_url,			   
	  dataType: "json",
	  success: function(results) {
		alert(result.name); 
// json data like {"name":"CreativeDev"}
      }
 });

Normally, you can use the dataType ‘json’ and leave it up to jQuery to do it for you.

2. jQuery method to parse JSON objects.

jQuery provides an another method “parseJSON”, which takes a well-formed JSON string and returns the resulting JavaScript object.

Let’s take a look at SYNTAX:

data = $.parseJSON(string);

Let’s look at what exactly JSON parsing:

 jQuery.ajax({
    url: dataURL,			   
    success: function(results) {
    var parsedJson = jQuery.parseJSON(results);    	              
	        alert(parsedJson.name); // alerts name value
	}
 });

As you can see, Here I have used parseJSON which parse the JSON String into an object so here you can not directly get value, you need to use an object to get the parsed value.

However, JSON is quite simple and easy to read and once you get used to the curly braces and brackets, not too hard to encode in PHP so you can use any of above method to parse the jQuery.

Thanks for reading and feel free to share your thoughts! Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.

Comments (1)

  1. plg post step wise on
    1 “how we can store form data to mysql table(with connection,proper position of code where to place in which file).
    and
    2 how to retrive it using json as middleware and frontend as php or HTML
    3how to load server data(json/mysql table) to specific web page.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

The Readers’ Poll – June 2012

Posted on 7 years ago

Bhumi

How to move Modx site to Live server

Posted on 6 years ago

Bhumi

To parse CSV Data in PHP

Posted on 7 years ago

Bhumi

How to use Short Hand Array in PHP

Posted on 4 years ago

Bhumi

Hello world

Updated 1 month ago

Nick Carter

In depth Understanding of Laravel Core

Updated 3 months ago

Bhumi

ECMAScript 6 New Features – 2

Updated 1 year ago

Bhumi

The Reader’s Poll – June 2015

Posted on 4 years ago

Bhumi

The Reader’s Poll – August 2014

Posted on 5 years ago

Bhumi

The Reader’s Poll – June 2014

Posted on 5 years ago

Bhumi

The Readers’ Poll – May 2014

Posted on 5 years ago

Bhumi

The Readers’ Poll – August 2013

Posted on 6 years ago

Bhumi