Online Snippets Library

Convert JSON String to JavaScript Object in jQuery

To convert json string to javascript object we will use jQuery.each() and JSON.parse().

Example

HTML
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"> </script>
<title>convert json string to javascript object </title>
</head>
<button>convert json string</button>
<div></div>
</html>
Javascript
$(document).ready(function(){
$("button").click(function(){
var json ='[{ "colorCode": "#f00" },{ "colorCode": "#0f0" },{ "colorCode": "#00f" }]';
jQuery.each(JSON.parse(json), function(idx, obj) {
$("div").append(obj.colorCode+'
'); }); }); });