← Back to PHP Course | Chapter 14: Advanced PHP | Lesson 3 of 24

PHP JSON Handling

JSON एक neat text format है जिसे computers information swap करने के लिए इस्तेमाल करते हैं, एक shopping list लिखने जैसा जिसे सब पढ़ सकें। PHP अपने data को उस text में बदल सकता है और text को वापस data में बदल सकता है।
Syntax
php
$json = json_encode($data);              // PHP value to JSON string
$object = json_decode($json);            // to stdClass object
$array = json_decode($json, true);       // to associative array

Data को JSON में Encode करना

json_encode() PHP arrays या objects को एक JSON string में बदल देता है, जो एक JavaScript frontend या किसी third-party API को structured data भेजने का standard format है।

उदाहरण: Encoding Data to JSON

php
<?php
// Declare `$data` as an array: `["name" => "Alice", "age" => 30]`
$data = ["name" => "Alice", "age" => 30];
// Print `json_encode($data)` to the output
echo json_encode($data);
?>

JSON को Objects में Decode करना

json_decode() एक JSON string को वापस PHP values में parse करता है; default रूप से यह nested stdClass objects produce करता है, इसलिए आप array bracket syntax के बजाय -> से fields access करते हैं।

उदाहरण: Decoding JSON to Objects

php
<?php
// Declare `$json`, set to '{"name": "Alice", "age": 30}'
$json = '{"name": "Alice", "age": 30}';
// Declare `$obj`, set to `json_decode($json)`
$obj = json_decode($json);
// Print `$obj->name` to the output
echo $obj->name;
?>

JSON को Associative Arrays में Decode करना

json_decode() के दूसरे argument की तरह true pass करना same JSON को objects के बजाय nested associative arrays में decode करता है, जो कई developers को PHP में काम करने के लिए ज़्यादा convenient लगता है।

उदाहरण: Decoding JSON to Associative Arrays

php
<?php
// Declare `$json`, set to '{"name": "Alice", "age": 30}'
$json = '{"name": "Alice", "age": 30}';
// Declare `$arr`, set to `json_decode($json, true)`
$arr = json_decode($json, true);
// Print `$arr['name']` to the output
echo $arr['name'];
?>

JSON Errors Handle करना

Malformed JSON json_decode() को चुपचाप null return करवाता है, इसलिए decoded result पर भरोसा करने से पहले json_last_error() check करना (या human-readable reason के लिए json_last_error_msg() पढ़ना) ज़रूरी है।

उदाहरण: Handling JSON Errors

php
<?php
// Declare `$json`, set to '{invalid json}'
$json = '{invalid json}';
// Declare `$result`, set to `json_decode($json)`
$result = json_decode($json);
// Check whether `json_last_error() !== JSON_ERROR_NONE`
if (json_last_error() !== JSON_ERROR_NONE) {
    // Print `"Error: " . json_last_error_msg()` to the output
    echo "Error: " . json_last_error_msg();
}
?>

JSON Encoding Options

json_encode() को JSON_PRETTY_PRINT flag pass करना output में indentation और line breaks add करता है, जो API responses debug करने के लिए उपयोगी है लेकिन production में छोटे payloads के लिए आमतौर पर skip किया जाना चाहिए।

उदाहरण: JSON Encoding Options

php
<?php
// Declare `$data` as an array: `["name" => "Alice", "age" => 30]`
$data = ["name" => "Alice", "age" => 30];
// Print `json_encode($data, JSON_PRETTY_PRINT)` to the output
echo json_encode($data, JSON_PRETTY_PRINT);
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. json_decode के बाद json_last_error() check न करना, ताकि invalid JSON चुपचाप null return करे।
  2. एक associative array पाने के लिए दूसरा argument true भूल जाना, और फिर एक object पर array syntax इस्तेमाल करना।
  3. ऐसा text encode करना जो valid UTF-8 नहीं है, जो json_encode को false return करवाता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.