Showing posts with label json. Show all posts
Showing posts with label json. Show all posts

Sunday, March 29, 2015

Deserialization in JSON

JSON being a data exchange and interchange format, this was designed for running in JavaScript on websites. Serialization of JSON can be done in many of modern web programming and developing languages like PHP, ASP.NET, PERL, Python etc. JSONpickle is a python library for serialization and deserialization of complex python objects to and from JSON. Similarly ASP.NET, PHP etc. has their own libraries and methods for deserialization of data/objects  in JSON.

Below is a basic example that is simple enough to explain about deserialization of JSON using PHP as a root language.

PHP Script (deserialization.php)



 <?PHP  
 $json='{"Andy":1,"Joe":2,"James":3,"Niraj":4,"Kumar":5,"Katrina":6}';  
 echo "<pre>";  
 var_dump(json_decode($json));  
 var_dump(json_decode($json,true));  
 echo "</pre>";  
 ?>

JSON in PERL explained with example

Using JSON with PERL :


Dealing with JSON in Perl is more complex comparing to other as Perl being a type less language. However, JSON can be either encoded or decoded using PERL for this operation, JSON module must be installed first.JSON-2.53.tar.gz searching it would let you get a module for PERL of JSON. You will find JSON functions to operate with JSON data using PERL.

JSON Function:

Encode_json : It converts PERL structure which is given in to a UTF-8 encoded, binary string.
syntax:
$json_text = encode_json ($perl_scalar );
or
$json_text = JSON->new->utf8->encode($perl_scalar);


Example for encoding JSON in PERL:

Perl File (encode.pl)
#!/perl/bin/perl  
 print "Example of encode\n";  
 use JSON;  
 my %rec_hash = ('PHP' => 1, 'SEO' => 2, 'JS' => 3, 'JSON' => 4, 'Project' => 5);  
 my $json = encode_json \%rec_hash;  
 print "$json\n";  
 Decode_json : It decodes the JSON string.  
 syntax:  
 $perl_scalar=decode_json $json_text  
 or  
 $perl_scalar= JSON->new->utf8->decode($json_text)  

Example for decoding JSON in PERL:

Perl File (decode.pl)
 #!/perl/bin/perl  
 print "Example of decode\n";  
 use JSON;  
 use Data::Dumper;  
 $json = '{"JSON":1,"JavaScript":2,"SEO":3,"HTML":4,"CSS":5}';  
 $content = decode_json($json);  
 print Dumper($content); 



Friday, March 13, 2015

Validate JSON

Using JSON validate validation of  your JSON data can be done. You can either validate your JSON document online or can download a copy and install it in your local machine for validation purpose.

Example:

[NOTE: please validate the script from any tools found in internet]
 [  
  {  
     "title": "PHP",  
     "description": "Hypertext Preprocessor"  
   },  
 {  
     "title": "JSON",  
     "description": "JavaScript Object Notation"  
   },  
   {  
     "title": "HTML",  
     "description": "Hypertext Markup Language"  
   },  
   {  
     "title": "CSS",  
     "description": "Cascading Style Sheet"  
   }  
 ]

JSON parse() method

Using JSON.parse() method you can parse a JSON .  JSON creates JavaScript objects ,using JavaScript to parse the data  is easy and mostly used by developers. If you are using PHP as your major language you can also use JSONencode() and JSONdecode() to parse a JSON string/object. Similarly according to language used JSON parsing differs and JSON can be used and parse.

Example: 

 <!DOCTYPE html>  
 <html>  
 <head>  
 <title>JSON Tutorials</title>  
 </head>  
 <body>  
         <h1>An Example of JSON Parse</h1>  
         <scriptsrc="http://code.jquery.com/jquery-1.7.1.min.js"></script>  
         <h2>Please Visit</h2>  
         <div id = "id1"></div>  
 <script type="text/javascript">  
 varobj = $.parseJSON( '{ "Course": "JSON","URL":"http://nirajanghimireyworkshop.blogspot.com/search/label/json" }' );  
 alert( "Do you like "+obj.Course+" ?");  
 document.getElementById("id1").innerHTML = "<a href = "+obj.URL+" /> "+obj.Course+" </a>"  
 </script>  
 </body>  
 </html> 

What are JSON Files ?

Files in JSON  are in ".json" format . These files contains the data that are to be interchanged while a client and a server communicate with one another. The data inside JSON files are in human-readable format so it is easy to read the data present inside a JSON file.

Example:



JSON File (demo.json)

 [  
 {  
 "Name": "PHP Blog",  
 "url": "http://www.nirajanghimireyworkshop.bogspot.com/search/label/php"  
 },  
 {  
 "Name": "JSON Blog",  
 "url": "http://www.nirajanghimireyworkshop.bogspot.com/search/label/json"  
 },  
 {  
 "Name": "Bootstrap Blog",  
 "url": "hhttp://www.nirajanghimireyworkshop.bogspot.com/search/label/Bootstrap"  
 }  
 ] 

HTML Document (json_files.html)

 <!DOCTYPE html>  
 <html>  
 <head>  
 <title>JSON</title>  
 </head>  
 <body>  
 <h1>An Example of JSON Files</h1>  
 <div id="id1"></div>  
 <script>  
  var tutorials = "demo.json";  
 myFunction(tutorials);  
 functionmyFunction(arr) {  
 var out = "";  
 vari;  
 for(i = 0; i<arr.length; i++) {  
 out += '<a href="' + arr[i].url + '">' +  
 arr[i].Name + '</a><br>';  
   }  
 document.getElementById("id1").innerHTML = out;  
 }  
 </script>  
 </body>  
 </html>

JSON Syntax

Syntax of JSON is similar to JavaScript syntax, as JSON is subset of JavaScript. Syntax in JSON has data that is represented in name/value pairs.

Standard for syntax in JSON:


  • Data in JSON is represented in name/value pairs.
  • Inside curly bracket object and names are followed by ‘ ’ (colon), the name and value are separated by , (comma).
  • Square holds values and arrays separated by, (comma).


Example:

 {  
 "cars"  
 [{  
 "name" : "Mercedis",  
 "color" : "Red",  
 "price" : "$5087890"  
 },  
 {"name" : "BMW",  
 "color" : "Black",  
 "price" : "$72893205"  
 }]  
 } 


JSON Objects

 Objects in JSON can be created using JavaScript, here objects can be anything. It can be unordered set of name or value pairs. Defining object in JSON has some rules and  while defining JSON Object it must be kept in notice that objects are always enclosed in curly braces starting with { and ending with } , name are followed by ‘.’(colon) and they are separated by ,(comma).

Syntax:

       var json_obj = {};  

Example:

 <!DOCTYPE html>  
 <html>  
 <head>  
 <title>JSON Object</title>  
 </head>  
 <body>  
         <h1>An Example of JSON Object</h1>  
 <p>Website Name: <h4 id="demo1"></h4></p>  
 <p>Website URL: <h4 id ="demo2"></h4></p>  
 <script language="javascript">  
 varjson_obj = { "Name" : "Workshop", "url":"www.nirajanghimireyworkshop.blogspot.com.com"};  
 document.getElementById("demo1").innerHTML = json_obj.Name;  
 document.getElementById("demo2").innerHTML = json_obj.url;  
 </script>         
 </body>  
 </html>  

JSON Format

JSON refers to JavaScript Object Notation and it is an open standard format, it uses human-readable text to transmit data object that contains attribute or values. JSON is mostly used in between server and client for transmission of data.
Syntax:

 [  
 {  
 name1:value1,  
 name2:value2,  
 nameN:valueN  
 },  
 {  
 name1:value1,  
 name2:value2,  
 nameN:valueN  
 }  
 ] 

Example:


HTML Document (format.html)

 <!DOCTYPE html>  
 <html>  
 <head lang="en">  
 <meta charset="UTF-8"/>  
 <title>JSON</title>  
 </head>  
 <body>  
 <h2>Example of JSON Format</h2>  
 <p id="content"></p>  
 <script>  
 var courses = [  
 {  
 "title":"HTML",  
 "description":"Hypertext Markup Languag"  
 },  
 {  
 "title":"JSON",  
 "description":"JavaScript Object Notation"  
 },  
   {  
     "title":"PHP",  
     "description":"Hypertext Preprocessor"  
   }  
 ];  
 document.getElementById("content").innerHTML =  
     courses[0].title + ": " + courses[0].description + "<br/>" +  
     courses[1].title + ": " + courses[1].description + "<br/>" +  
     courses[2].title + ": " + courses[2].description;;  
 </script>  
 </body>  
 </html>  

Array in JSON

While defining array in JSON it has some protocols, they are enclosed with square brackets and the vales inside an array are separated by comma, unlike other indexing of an array can start form 0 or 1 here. JSON array is well ordered sequence of values.

Protocol for declaring an array in JSON:

  • An array must be wrapped in square brackets, i.e. “[“in starting and “]” at end of an array.
  • Commas”,” must be used to separate the values.
  • Indexing in an array can start with 0 or 1.
  • If the key names are sequential integers array must be used.
Syntax:

 [ value1, value2,.......,value n]  


Example:

HTML Document (array.html)
 <!DOCTYPE html>  
 <html>  
 <head lang="en">  
 <meta charset="UTF-8">  
 <title>json</title>  
 </head>  
 <body>  
 <h2>Example of JSON Array</h2>  
 <p id="content"></p>  
 <p id="content1"></p>  
 <script>  
 var info = '{"courses":[' +  
       '{"title":"HTML","description":"HyperText Markup Language" },' +  
       '{"title":"CSS","description":"Cascading Style Sheet" },' +  
       '{"title":"JSON","description":"JavaScript Object Notation" }]}';  
 obj = JSON.parse(info);  
 document.getElementById("content").innerHTML =  
       "Title: " + obj.courses[0].title + ": " + "<br/> Description: " + obj.courses[0].description;  
 obj = JSON.parse(info);  
 document.getElementById("content1").innerHTML =  
       "Title: " + obj.courses[2].title + ": " + "<br/> Description: " + obj.courses[2].description;  
 </script>  
 </body>  
 </html>

Wednesday, December 24, 2014

Encoding and Decoding JSON data

JSON is a  kind of data exchanging format ,encoding and decoding  can be done in various web programming languages using JSON like PHP, ASP.NET etc. Here is a basic example on PHP about  encoding and decoding .

Encoding JSON data in PHP :




Example: 

PHP File (encode.php)
 <?PHP  
 header('Content-type: application/json');  
 $json = array('PHP' => 1, 'JSON' => 2, 'HTML' => 3, 'JavaScript' => 4, 'jQuery' => 5);  
 echo json_encode($json);  
 ?>  

PHP File (decode.php)
 <?PHP  
 $json='{"PHP":1,"JSON":2,"HTML":3,"CSS":4,"JavaScript":5,"jQuery":6}';  
 echo "<pre>";  
 var_dump(json_decode($json));  
 var_dump(json_decode($json,true));  
 echo "</pre>";  
 ?> 

Tuesday, December 23, 2014

Serialization and its meaning in JSON

Serialization and its meaning in JSON:


Serialization in JSON is similar to parsing; serialization of JSON can be done in almost all modern programming languages like PHP, ASP.NET etc. Serialization procedure of JSON differs to languages used. JSONpickle is a python library for serialization and Deserialization of complex python objects to and from JSON. Similarly ASP.NET, PHP etc. has their own libraries and methods for serialization of data/objects in JSON.
Syntax:
Example:
PHP Script (json.php)
 <?PHP   
  if($_SERVER['REQUEST_METHOD'] == "POST") {   
   // Gets data from form   
   $name = $_POST['username'];   
   $password = $_POST['password'];   
   // checks if condition matches or not   
   if ($name == $password) {   
    $json = array("status" => 200, "msg" => "Sucessfully logged in");   
   } else {   
    $json = array("status" => 400, "msg" => "Username and Password do not match");   
   }   
  }else {   
   $json = array("status" => 404, "msg" => "Request method not accepted");   
  }   
  /* Output header */   
  header('Content-type: application/json');   
  echo json_encode($json);   
  //echo json_last_error() ; this gievs the last error found by json and 0 value count if not found   
  ?> 

HTML Document (serialization.html)
  <!DOCTYPE html>   
  <html lang="en">   
  <head>   
  <meta charset="utf-8"/>   
  <title>Nirajan Ghimirey's Workshop</title>   
  </head>   
  <body>   
  <div align="center">   
  <h2>Please insert the data to see the encoded JSON message</h2>   
  <form method="post" action="json.php">   
  <input type="text" name="username" placeholder="Username"><br/><br/>   
  <input type="password" name="password" placeholder="Password"><br/><br/>   
  <input type="submit" value="Login">   
  </form>   
  </div>   
  </body>   
  </html>