QuizCure
Find Available Solution Here!

json_encode

Deepak Jul 26, 2024

Json_encode is a php inbuilt function used for Encoding purpose to to achieve data usability. It encode passed php values into corresponding json format.

Syntax:
json_encode ( mixed $param , int $flags = 0 , int $depth = 512 )


Input Params:
  • $param : Required
  • $flags : Optional (Default: 0). This is JSON constants . Example : JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_INVALID_UTF8_IGNORE..
  • $depth : Optional param and must be greater than zero. Example : Depth one is considered for one Dimensional array. Depth N is considered for N Dimensional array. Default depth 512. we can set maximum depth. Json_encode will return false if specified depth is less than size of input array. Please refer below example for more understanding.

Return Value:
  • On Success : Json Encoded String
  • On Failure : False

Let's Explore some Example below

Case 1: Json encode Value for One Dimensional Array with Depth 1

$employee1 = array("John", "Denis", "Peter");

var_dump (json_encode($employee1, 0,  1));

//Return value: string(24) "["John","Denis","Peter"]"

Case 2: Json encoded Value for Two Dimensional Array with Depth less than input array size

$employee2 = array( "John" => array("Account"), "Denis" => array("Finance"), "Peter" => array("Hr"));

var_dump( json_encode($employee2, 0,  1));

//Return value: bool(false)

Case 3: Json encode Value for Two Dimensional Array with Depth more than size of array (Ex. 3)

$employee3 = array( "John" => array("Account"), "Denis" => array("Finance"), "Peter" => array("Hr"));


var_dump( json_encode($employee3, 0,  3));

//Return value: string(55) "{"John":["Account"],"Denis":["Finance"],"Peter":["Hr"]}"

How to find root cause and solve if json_encode returning false?

There can be various reasons behind false return from json_encode. Let's see some of most common error as below

JSON error codes Error message Available on PHP version
JSON_ERROR_DEPTH The maximum exceeded. (Please refer example below.) PHP 5.3.0
JSON_ERROR_SYNTAX Syntax error in provided input data PHP 5.3.0

Sometimes we can't predict what is the issue in input data. No issues, we can use following json in built method to know actual cause

  • json_last_error() return int value.
  • Json_last_error_msg() returns the error string for last call of json encode/decode function.

Let see by an example below

Get Json encode Value for Two Dimensional Array with given depth less than input array size

  $employee2 = array( "John" => array("Account"), "Denis" => array("Finance"), "Peter" => array("Hr"));

    var_dump( json_encode($employee2, 0,  1));

    //output : bool(false)

    var_dump(json_last_error() == JSON_ERROR_DEPTH);

      //output : bool(true)

    var_dump("Error Message:" . json_last_error_msg());
    
     //output : Error Message: Maximum stack depth exceeded

How to get the json encoded value as an original string then \uXXXX for special chars/foreign language?

Let's understand with following example

Case 1: Json_encode foreign language
 $language = array("hindi" => "??????", "chinese" => "??", "japanese"=> "?????", "vietnamese"=> "xin chào");      


echo json_encode($language);

Output : {"hindi":"\u0928\u092e\u0938\u094d\u0924\u0947","chinese":"\u4f60\u597d","japanese":"\u3053\u3093\u306b\u3061\u306f","vietnamese":"xin ch\u00e0o"}

Case 2: Json_encode foreign language with JSON_UNESCAPED_UNICODE
 $language = array("hindi" => "??????", "chinese" => "??", "japanese"=> "?????", "vietnamese"=> "xin chào");     
 
echo json_encode($language, JSON_UNESCAPED_UNICODE);

Output: {"hindi":"??????","chinese":"??","japanese":"?????","vietnamese":"xin chào"}


Was this post helpful?

Send Feedback

Connect With QuizCure


Follow Us and Stay tuned with upcoming blog posts and updates.

Contributed By

Deepak

Deepak

QC STAFF
51 Posts
  • PHP
  • JAVA
  • PYTHON
  • MYSQL
  • SEO

You May Like to Read

Scroll up