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

javascript encode json using JSON.stringify(), By using the JSON.parse( ). It’s JSON Encode is used to parse JSON string as well as return JavaScript value or an object specified by input string.

javascript encode json

Parse the data with JSON.parse(). The method JSON.stringify(members) takes the object as well as converts it into a string.

js encode json
object to json javascript

var members={"first_name":"Pakainfo","last_name":"Website","age":45};
var membersJSONString=JSON.stringify(members); 

Types of JSON

  • Object
  • Array
{
    "book": {
        "name": "All availabble pakainfo history",
        "owner": "L. V. gandhi",
        "year": 2025,
        "genre": "Rukamni sattustion",
        "bestseller": true
    }
}

Parsing JSON Data in JavaScript

Example

{"name": "Pakainfo", "age": 22, "address": "India"}

By using the JSON.parse( )

first of all Store JSON data in a JS variable and then Converting JSON-encoded string to JS object and Accessing individual value from JS object
Example

var json = '{"name": "Pakainfo", "age": 22, "address": "India"}';
 
var results = JSON.parse(json);
 
alert(results.name); // Results: Pakainfo
alert(results.age); // Results: 22
alert(results.address); // Results: India

Don’t Miss : JSON encoder and decoder

Parsing Nested JSON Data in JavaScript

Example

/* Storing multi-line JSON string in a JS variable
using the new ES6 template literals */
var json = `{
    "book": {
        "name": "All availabble pakainfo history",
        "owner": "L. V. gandhi",
        "year": 2025,
        "members": ["Sejal Ramanai", "krishna pandya", "kinara thummer"],
        "genre": "Rukamni sattustion",
        "price": {
            "paperback": "$10.40", "hardcover": "$20.32", "kindle": "$4.11"
        }
    }
}`;
 
// Converting JSON object to JS object
var obj = JSON.parse(json);
 
// Define recursive function to print nested values
function printValues(obj) {
    for(var k in obj) {
        if(obj[k] instanceof Object) {
            printValues(obj[k]);
        } else {
            document.write(obj[k] + "
"); }; } }; // Displaying all the values from the resulting object printValues(obj); document.write("
"); // Displaying a single value document.write(obj["book"]["owner"] + "
"); // Prints: L. V. gandhi document.write(obj["book"]["members"][0] + "
"); // Prints: Sejal Ramanai document.write(obj["book"]["price"]["hardcover"]); // Prints: $20.32

Encoding Data as JSON in JavaScript

Stringify a JavaScript Object

// Sample JS object
var obj = {"name": "Pakainfo", "age": 22, "address": "India"};
 
// Converting JS object to JSON string
var json = JSON.stringify(obj);
alert(json);

Results

{"name":"Pakainfo","age":22,"address":"India"}

Stringify a JavaScript Array
Converting JS array to JSON string

var res = ["Pakainfo", "infinityknow", "Laravel", "PHP", "Vuejs"];
 
var json = JSON.stringify(res);
alert(json);

Result

["Pakainfo","infinityknow","Laravel","PHP","Vuejs"]

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

Leave a Comment