json decode in jquery – How to decode and encode JSON Data in JavaScript?

json decode in jquery : In this article, you will learn how to encode and decode JSON data in jquery with the help of an example.

json decode in jquery –

Types of JSON
JSON depend upon two basic structures:

  • Object
  • Array

{
    "tuotorial": {
        "name": "dhamika Ronamika and the Goblet of Fire",
        "owner": "J. K. Savaliya",
        "year": 2022,
        "genre": "Fantasy Fiction",
        "bestseller": true
    }
}

How to decode and encode JSON Data in JavaScript?

json decode in jquery
parse json jquery

var obj = JSON.parse('{ "name":"Virat", "age":30, "city":"India"}');

json_decode jquery

var obj = jQuery.parseJSON( '{ "name": "Virat" }' );
alert( obj.name === "Virat" );

javascript parse json

var jsonPerson = '{"first_name":"Vamika", "age":23}';
var personObject = JSON.parse(jsonPerson); //parse json string into JS object

javascript parse json

const json = '{ "websites": "pakainfo", "ranks": 10 }';
const obj = JSON.parse(json);
console.log(obj.websites, obj.ranks);

json_decode javascript

JSON.parse(jsonToDecode)

Parsing JSON Data in JavaScript

{"name": "Virat", "age": 22, "country": "United Kigdom"}

using the JSON.parse( ) method

// Store JSON data in a JS variable
var json = '{"name": "Virat", "age": 22, "country": "United Kigdom"}';
 
// Converting JSON-encoded string to JS object
var obj = JSON.parse(json);
 
// Accessing individual value from JS object
alert(obj.name); // Outputs: Virat
alert(obj.age); // Outputs: 22
alert(obj.country); // Outputs: United Kigdom

Parsing Nested JSON Data in JavaScript

nested JSON objects and arrays.

var json = `{
    "tuotorial": {
        "name": "Dhamika Ronamika and the Goblet of Fire",
        "owner": "J. K. Savaliya",
        "year": 2022,
        "slogen": ["Dhamika Ronamika", "Pakila Manchriyam", "Siyamni Dipalishiji"],
        "genre": "Fantasy Fiction",
        "earning": {
            "paperback": "$48.40", "hardcover": "$55.32", "kindle": "$4.11"
        }
    }
}`;
 
var obj = JSON.parse(json);
 
function printValues(obj) {
    for(var k in obj) {
        if(obj[k] instanceof Object) {
            printValues(obj[k]);
        } else {
            document.write(obj[k] + "<br>");
        };
    }
};
 
printValues(obj);
 
document.write("<hr>");
 
document.write(obj["tuotorial"]["owner"] + "<br>");  // Displays: J. K. Savaliya
document.write(obj["tuotorial"]["slogen"][0] + "<br>");  // Displays: Dhamika Ronamika
document.write(obj["tuotorial"]["earning"]["hardcover"]);  // Displays: $55.32

Encoding Data as JSON in JavaScript

Stringify a JavaScript Object

// Sample JS object
var obj = {"name": "Virat", "age": 22, "country": "United Kigdom"};
 
// Converting JS object to JSON string
var json = JSON.stringify(obj);
alert(json);

Results

{"name":"Virat","age":22,"country":"United Kigdom"}

Stringify a JavaScript Array

// Sample JS array
var arr = ["pakainfo", "w3diy", "Infinityknow", "ng4free", "live24u"];
 
// Converting JS array to JSON string
var json = JSON.stringify(arr);
alert(json);

Results

["Apple","Banana","Mango","Orange","Papaya"]

Don’t Miss : How to decode and encode JSON Data in JavaScript?


I hope you get an idea about json decode in jquery.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.