php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45563 json_decode fails on json representing javascript objects
Submitted: 2008-07-19 13:00 UTC Modified: 2008-07-22 15:06 UTC
From: coyote4til7 at gmail dot com Assigned:
Status: Not a bug Package: JSON related
PHP Version: 5.2.6 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
37 - 24 = ?
Subscribe to this entry?

 
 [2008-07-19 13:00 UTC] coyote4til7 at gmail dot com
Description:
------------
When you encode a proper javascript object as json and then use the json_decode function, it fails.

The keys in Javascript objects are _not_ supposed to be quoted.  If you're going to store a json-encoded object (say in a database) and then reuse it later in javascript, quoting the keys so that php's json_decode function works means that you're counting on the browser's javascript to support something that is not supported by the javascript specification.

Reproduce code:
---------------
// In javascript, we have an object:
// var obj = { parent_id:-1,label:"label" };
//
// In javascript, the keys for objects are _not_ quoted by 
// single or double quotes.  So, if we're going to properly encode
// this to later reuse it in javascript, we get this sent to php:
$json = '[{parent_id:-1,label:"label"}]';

// Let's try to convert it:
var_dump(json_decode($json, true));

Expected result:
----------------
{ ["parent_id"]=>  int(-1) ["label"]=>  string(5) "label" }

Actual result:
--------------
NULL

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-07-22 14:03 UTC] jani@php.net
Actually you're wrong. From the RFC 4627:

object = begin-object [ member *( value-separator member ) ] end-object
member = string name-separator value
string = quotation-mark *char quotation-mark
 [2008-07-22 14:04 UTC] jani@php.net
Note: This is about JSON, it's not about Javascript.
 [2008-07-22 15:06 UTC] coyote4til7 at gmail dot com
Jani, please read the definition of string in the spec.  Strings do not have to be quoted for JSON.  That includes member (names).

Since JSON can be and is parsed via Javascript's eval (in fact that was the method everyone used in the beginning and that is still used in certain circumstances) and JavaScript's spec specifies that object names are _not_ to be quoted, php's code can handle JSON that's coded properly to make sure it doesn't blow up a strictly implemented JavaScript interpreter.

So, let's see if I can summarize.  PHP's JSON support isn't about JavaScript and so it doesn't support a major real-world use case.  So, the JSON support in PHP is a fairly masturbatory exercise, no?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 08:02:42 2024 UTC