what is json_Encode and json_Decode function in PHP?

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours scrolling social media and waste money on things we forget, but won’t spend 30 minutes a day earning certifications that can change our lives.
Master in DevOps, SRE, DevSecOps & MLOps by DevOps School!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

json_Encode() Function

 The json_encode() function is an inbuilt function in PHP which is used to convert PHP array into JSON representation, this can return a JSON encoded string on success or false on failure.

syntax

string json_encode( $value )

example

<?php
   $post_data = array(
      "item" => array(
            "item_type_id" => 1,
            "tring_key" => "AA",
            "string_value" => "Hello",   
            "string_extra" => "App",
            "is_public" => 1,
            "is_public_for_contacts" => 0
      )
   );
   echo json_encode($post_data)."\n";
?>

output

{"item":{"item_type_id":1,"tring_key":"AA","string_value":"Hello","string_extra":"App","is_public":1,"is_public_for_contacts":0}}

json_Decode() function

The json_decode() function is an inbuilt function in PHP which is used to decode a JSON string. It converts a JSON encoded string into a PHP variable.

syntax

json_decode( $json, $assoc = FALSE )

example

<?php
  
$data = '{"g":7, "e":5, "e":5, "k":11, "s":19}';
  
var_dump(json_decode($data, true));
  
?>

output

array (size=4)
  'g' => int 7
  'e' => int 5
  'k' => int 11
  's' => int 19

Related Posts

Uncovering Chennai: A Traveler and Local Perspective on City Life, Events and Landmarks

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

Modernizing Tech Stacks for Scalable Gaming Enterprises

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

Essential Qualities and Competencies to Look for in a DevOps Instructor

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

Exploring Your Backyard: Finding Local Goods, Shops, and Services

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

Finding Your Way Through Oncology Care: A Patient and Family Roadmap

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

Finding Excellence in Neurological Care: Selecting a Specialized Neurosurgery Facility

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x