Breaking

Monday, February 10, 2020

JSON in PHP

JSON : JavaScript Object Notation

Before jumping right into the details of the JSON, let's understand different data interchange formats present. So, data interchange format is nothing but the format with which the Client and server communicate with each other.

There are mostly 2 types of such formats.
  • JSON
  • XML

We will look into the XML data interchange format later. Here, we'll first understand what is JSON and the related information about it.

What is JSON?

As mentioned above, JSON is the JavaScript Object Notation. Here, you will see two main words i.e., JavaScript & Object. There is a relation between these two words while understanding JSON. So JSON is the extension of the JavaScript and it uses object notation (similar kind of).

Let's take an example of an object in javascript:
var person = {firstName:"Jon", lastName:"Brown"};

Object is nothing but a special type of variable (which holds special meaning) and it contains different types of values stored in it as key-value pair inside the curly bracket.

Now, let's take an example of JSON.
var person = {"firstName":"Jon", "lastName":"Brown"};

JSON also has key-value pair but the observational difference is that keys in object are not wrapped in quotes while JSON does have that and also javascript object's each value can be of any datatype including a function in which JSON is lacking.

Here is the example of a JavaScript object with function as value-
var person = {
   firstName:"Jon",
   lastName:"Brown",
   getNameInfo:function() {
      alert(this.firstName);
   }
};

Where this refers to the current object.

What are the advantages of JSON?

  • JSON format is easy to understand and use.
  • It is faster as compared to other data interchange format like XML.
  • JSON provides wide range of supported browser compatibility.
  • JSON is faster in terms of parsing at server side.

How to use JSON in PHP?

PHP has provided an inbuilt function to handle JSON. There are mainly two functions which play and important role while working with JSON string

  • json_encode();
  • json_decode();

1. What is json_encode() ?

json_encode ( mixed $value [, int $options = 0 [, int $depth = 512 ]] ) : string

parameters:

$value - The value which is being encoded. Data must be UTF-8 encoded. | Required

$options - Bitmask consisting of JSON_FORCE_OBJECT, JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_INVALID_UTF8_IGNORE, JSON_INVALID_UTF8_SUBSTITUTE, JSON_NUMERIC_CHECK, JSON_PARTIAL_OUTPUT_ON_ERROR, JSON_PRESERVE_ZERO_FRACTION, JSON_PRETTY_PRINT, JSON_UNESCAPED_LINE_TERMINATORS, JSON_UNESCAPED_SLASHES, JSON_UNESCAPED_UNICODE, JSON_THROW_ON_ERROR. | optional

It is basically a string representation of the value/variable provided. This value OR variable can be of any type except resource. This means value/variable can either be integer, float, boolean, array etc. But primarily value provided to the json_encode() function an array.

Let's take a few examples of json_encode() function on different data-types-
// Using string data-type
var_dump(json_encode("equal"));

// Using boolean data-type
var_dump(json_encode(true));

// Using Integer data-type
var_dump(json_encode(1234));

// Using float data-type
var_dump(json_encode(12.34));
// output
string(7) ""equal""
string(4) "true"
string(4) "1234"
string(5) "12.34"


No comments:

Post a Comment