JavaScript JSON object code example
Example 1: change js to json
var obj = {name: "Martin", age: 30, country: "United States"};
var json = JSON.stringify(obj);
console.log(json);
"https://www.tutorialrepublic.com/faq/how-to-convert-js-object-to-json-string.php"
Example 2: Javascript object to JSON string
var person={"first_name":"Tony","last_name":"Hawk","age":31};
var personJSONString=JSON.stringify(person);
Example 3: json javascript
myObj = {name: "John", age: 31, city: "New York"};
myJSON = JSON.stringify(myObj);
localStorage.setItem("testJSON", myJSON);
text = localStorage.getItem("testJSON");
obj = JSON.parse(text);
document.getElementById("demo").innerHTML = obj.name;
Example 4: javascript parse json
var jsonPerson = '{"first_name":"billy", "age":23}';
var personObject = JSON.parse(jsonPerson);
Example 5: key value json javascript
var jsonArray = '{"required":1, "minlength":2}'
var jsonParsedArray = JSON.parse(jsonArray);
for (key in jsonParsedArray) {
if (jsonParsedArray.hasOwnProperty(key)) {
console.log("%c "+key + " = " + jsonParsedArray[key],"color:cyan");
}
}
Example 6: json object
var myObj, x;
myObj = {"name":"John", "age":30, "car":null};
x = myObj.name;
document.getElementById("demo").innerHTML = x;