json array in javascript code example

Example 1: change js to json

var obj = {name: "Martin", age: 30, country: "United States"};
 
// Converting JS object to JSON string
var json = JSON.stringify(obj);
 
console.log(json);
// Prints: {"name":"Martin","age":30,"country":"United States"} 
"https://www.tutorialrepublic.com/faq/how-to-convert-js-object-to-json-string.php"

Example 2: javascript parse json

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

Example 3: js string to json

var obj = JSON.parse("{no:'u',my:'sql'}");//returnes {no:'u',my:'sql'}

Example 4: json arrays

{
  "array": [{1,2,3,4,5,6}]
}

Example 5: js create json array

var employees = {
    accounting: []
};

for(var i in someData) {    

    var item = someData[i];   

    employees.accounting.push({ 
        "firstName" : item.firstName,
        "lastName"  : item.lastName,
        "age"       : item.age 
    });
}

Example 6: hwo to access arary in json

console.log(dataJS);
console.log(dataJS[0].eee1);
console.log(dataJS[0].comments[0]);