What is json decode and encode in php?
❮ PHP JSON Reference Show ExampleStore JSON data in a PHP variable, and then decode it into a PHP object: $jsonobj = '{"Peter":35,"Ben":37,"Joe":43}'; var_dump(json_decode($jsonobj)); Run Example » Definition and UsageThe json_decode() function is used to decode or convert a JSON object to a PHP object. Syntaxjson_decode(string, assoc, depth, options) Parameter Values
Technical Details
More ExamplesExampleStore JSON data in a PHP variable, and then decode it into a PHP associative array: $jsonobj = '{"Peter":35,"Ben":37,"Joe":43}'; var_dump(json_decode($jsonobj, true)); Run Example » ExampleHow to access the values from the PHP object: $jsonobj = '{"Peter":35,"Ben":37,"Joe":43}'; $obj = json_decode($jsonobj); echo $obj->Peter; Run Example » ExampleHow to access the values from the PHP associative array: $jsonobj = '{"Peter":35,"Ben":37,"Joe":43}'; $arr = json_decode($jsonobj, true); echo
$arr["Peter"]; Run Example » ❮ PHP JSON Reference (PHP 5 >= 5.2.0, PHP 7, PHP 8, PECL json >= 1.2.0) json_decode — Decodes a JSON string Descriptionjson_decode( Parametersjson The This function only works with UTF-8 encoded strings.
associative When depth Maximum nesting depth of the structure being decoded. The value must be greater than flags Bitmask of Return Values Returns the value encoded in Errors/Exceptions If Changelog
ExamplesExample #1 json_decode() examples
The above example will output: object(stdClass)#1 (5) { ["a"] => int(1) ["b"] => int(2) ["c"] => int(3) ["d"] => int(4) ["e"] => int(5) } array(5) { ["a"] => int(1) ["b"] => int(2) ["c"] => int(3) ["d"] => int(4) ["e"] => int(5) } Example #2 Accessing invalid object properties Accessing elements within an object that contain characters not permitted under PHP's naming convention (e.g. the hyphen) can be accomplished by encapsulating the element name within braces and the apostrophe.
= '{"foo-bar": 12345}';$obj = json_decode($json); Example #3 common mistakes using json_decode()
$bad_json = "{ 'bar': 'baz' }"; Example #4
The above example will output: array(1) { [1]=> array(2) { ["English"]=> array(2) { [0]=> string(3) "One" [1]=> string(7) "January" } ["French"]=> array(2) { [0]=> string(3) "Une" [1]=> string(7) "Janvier" } } } Last error: No error NULL Last error: Maximum stack depth exceeded Example #5 json_decode() of large integers
The above example will output: object(stdClass)#1 (1) { ["number"]=> float(1.2345678901235E+19) } object(stdClass)#1 (1) { ["number"]=> string(20) "12345678901234567890" } Notes
See Also
Lennart Hengstmengel ¶ 8 months ago
Alien426 ¶ 1 year ago
cubefox at web dot NOSPAMPLEASE dot de ¶ 1 year ago
greaties at ghvernuft dot nl ¶ 1 year ago
as-works at narod dot ru ¶ 10 months ago
Anonymous ¶ 1 year ago
= [0 => "foo", 1 => "bar", 2 => ["baz"]];$associative = false;var_dump( mattia ¶ 1 year ago
What is JSON encoding and decoding?JsonEncoder and JsonDecoder
A decoder is a function that takes a CharSequence and returns a Right with the decoded value or a Left with an error message. An encoder is a function that takes a value of type A and returns a CharSequence that represents the encoded value (JSON string).
What is JSON encode in PHP?json_encode() is a native PHP function that allows you to convert PHP data into the JSON format. json_encode ( mixed $value [, int $options = 0 [, int $depth = 512 ]] ) : string. The function takes in a PHP object ($value) and returns a JSON string (or False if the operation fails).
What is JSON decoding?The json_decode() function is used to decode or convert a JSON object to a PHP object.
What is JSON encoding?jsonencode encodes a given value to a string using JSON syntax. The JSON encoding is defined in RFC 7159. This function maps Terraform language values to JSON values in the following way: Terraform type. JSON type.
|