PHP's Object Notation: Accessing Fields/Members With Hyphens in Their Name

6cebef738df782280469e99e33ee1feb44c8d0e3

Wed Apr 22 18:59:59 2009 -0700

I fought through this issue today at work. In PHP, you might have a need to parse some JSON that contains a field/member name with one or more hyphens. Here’s an example:

{
  "field-one" : 1,
  "field-two" : 2,
  "field-three" : [ 1, 2, 3 ]
}

Most JSON libraries should convert this JSON string into a native object with ease. However, in PHP when you try to access $obj->field-one directly, PHP will complain given the hyphen in the member name. The solution to this simple problem, is to wrap the name in curly braces, like so:

$obj->{'field-one'}

Here’s a more complete example using Services_JSON in PHP:

<?php

require_once("JSON.php");
$json = new Services_JSON();

$string = "{\"field-one\":1,\"field-two\":2,\"field-three\":[1,2,3]}";

$obj = $json->decode( $string );

// Will FAIL
//echo $obj->field-one;

// WORKS
echo $obj->{'field-one'};

?>

Cheers.

php