How to work with JSON (JavaScript Object Notation)

In this article I will write about working with JSON. JSON stands for JavaScript Object Notation. It is a great way to send objects back and forth through client-server and also to create API’s that return JSON when information is requested. Lets look at an object in javascript.


var object = {
self : {name:"customer1",job:"cashier"},
todo :"jsonobject",
names : [{employ:"work",happy:true,work:"program"},
{employ:"job",happy:false,work:"intro"}]
};

This object, called object has a couple variables and an array names. Now lets see this object in JSON format.


{"self":{"name":"customer1","job":"cashier"},
"todo":"jsonobject",
"names":[{"employ":"work","happy":true,"work":"program"},
{"employ":"job","happy":false,"work":"intro"}]}

in javascript to turn an object into JSON you use the JSON.Stringify method

JSON.stringify(object);

And to turn JSON into and Object you use JSON.Parse method


var object = JSON.parse(json);

Now to work with JSON in php you can make objects in php and then use json_encode to turn the object into JSON like this


<?php $myObj = new \stdClass();
$myObj->name = "John";
$myObj->age = 30;
$myObj->city = "New York";
$myArray = array();
for($i = 0;$i < 10;$i++){
$myArray[$i] ="hello world".$i;
}
$myObj->hello = $myArray;
$myJSON = json_encode($myObj);
echo $myJSON;
?>

Now the variable $myJSON will look like this after you encode it


{"name":"John",
"age":30,
"city":"New York",
"hello":["hello world0","hello world1",
"hello world2","hello world3","hello world4",
"hello world5","hello world6","hello world7",
"hello world8","hello world9"]}

and if you are sending a JSON object to a php script using JSON.stringify you can grab the object in php with something like this using json-decode

$obj = json_decode($_POST["x"], false);

And that is pretty much the basics for using JSON in javascript and php. One note i was looking around and it appears that you are not supposed to send functions through JSON but there is a way that is not consistent so it shouldnt actually be done, as far as i know. I hope you enjoyed this article. Untill next time 🙂

A Cookie Helper object in JavaScript

This example is a little helper class to make and erase cookies in javascript. I will first post the Cookies.js script that will create, read, and erase cookies from your browser.

Cookies.js


/*
author: Copypasteearth 7/17/2017
*/

function COOKIE () {

this.createCookie = function (name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
,
this.readCookie = function (name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
,
this.eraseCookie = function(name) {
this.createCookie(name,"",-1);
}
}

Then you just include this javascript in a webpage and create the object and then you can call the different functions as you will. Here is an example HTML file using it. This file just pretty much tests out the Cookies.js file.

index.html


<!DOCTYPE html>

<html lang="en">
<head>
<meta charset="utf-8" />
<title>profile</title>
</head>

<body>

<script src="Cookies.js"></script>
<h1>this is a test of Cookies</h1>
<h2 id = "cook"></h2>
<input id = "cookiename" type="text" placeholder="cookie name..."></input>
<input id = "cookievalue" type="text" placeholder="cookie value..."></input>
<input id = "days" type = "number" placeholder="number of days"></input>
<button onclick="buttonClick()">Create Cookie</button>
<br>
<input id = "getcookie" placeholder="cookie name"></input>
<button onclick="getCookie()">Read Cookie</button>
<br>
<input id = "erase" placeholder="cookie name"></input>
<button onclick="deleteCookie()">Erase Cookie</button>
<p id= "paragraph"></p>
<script>
var Cookie = new COOKIE();
function buttonClick(){
var name = document.getElementById("cookiename").value;
var value = document.getElementById("cookievalue").value;
var days = document.getElementById("days").value;
Cookie.createCookie(name,value,days);
document.getElementById("paragraph").innerText = "Cookie Created: " + name + "=" + value + ";" + days;
}
function getCookie(){
var name = document.getElementById("getcookie").value;
var cookie = Cookie.readCookie(name);
document.getElementById("paragraph").innerText = "Cookie Read: " + name + "=" + cookie;
}
function deleteCookie(){
var name = document.getElementById("erase").value;
Cookie.eraseCookie(name);
document.getElementById("paragraph").innerText = "Cookie Erased: " + name;
}
</script>
</body>

</html>

and that’s pretty much it for this article. Maybe you can get some use out of this. I hope you enjoyed this article.

%d bloggers like this: