php json decode issue - values not quoted in string

Hi -
A third party is sending me a json string in which the values are not quoted for example:

$string='{"EventType":10,
"EventSubType":1001,
"UnitId":91957012,
"CustomerID": 1994043
"DeviceId":127,
"EventDateUtc":"2015-03-10T20:51:18.1390759Z",
"EventDesc":"System Closed by John Doe",
"UserDesc":"John Doe",
"ContactId":10724307,
"GroupId":11}'

When I try to use either $json_o=json_decode($string) or $json_a=json_decode($string,true)
I get no result for:

echo " o is " . $json_o->EventType;
echo " a is " . $json_a[EventType];

If I put quotes around the values I get correct results -

I can not ask the third party to change their post to me -
Is there some way around this or am I missing something ?

Thanks in advance for any help and/or insights -
Richard
rwinnickAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
You SHOULD ask the third party to change the post - it's malformed JSON.  Have a look at the line captioned CustomerID and you will see why.  It does not end with a comma.  Put in the trailing comma and it works correctly.

http://iconoun.com/demo/temp_rwinnick.php

<?php // demo/temp_rwinnick.php

/**
 * http://www.experts-exchange.com/questions/28696109/php-json-decode-issue-values-not-quoted-in-string.html
 */
error_reporting(E_ALL);
echo '<pre>';

// FROM THE POST AT EE - DOES NOT WORK
$str = <<<EOD
{"EventType":10,
"EventSubType":1001,
"UnitId":91957012,
"CustomerID": 1994043
"DeviceId":127,
"EventDateUtc":"2015-03-10T20:51:18.1390759Z",
"EventDesc":"System Closed by John Doe",
"UserDesc":"John Doe",
"ContactId":10724307,
"GroupId":11}
EOD;

// TRY TO MAKE AN OBJECT
$obj = json_decode($str);
echo PHP_EOL . htmlentities($str);
echo PHP_EOL;

// IF DECODING FAILS?
if (!$obj) echo json_last_error_message();
echo PHP_EOL;

// IMPROVED VERSION WITH A COMMA AT THE END OF CustomerID LINE
$str = <<<EOD
{"EventType":10,
"EventSubType":1001,
"UnitId":91957012,
"CustomerID": 1994043,
"DeviceId":127,
"EventDateUtc":"2015-03-10T20:51:18.1390759Z",
"EventDesc":"System Closed by John Doe",
"UserDesc":"John Doe",
"ContactId":10724307,
"GroupId":11}
EOD;

// TRY TO MAKE AN OBJECT
$obj = json_decode($str);
echo PHP_EOL . htmlentities($str);
echo PHP_EOL;

// IF DECODING FAILS?
if (!$obj) echo json_last_error_message();
var_dump($obj);

function json_last_error_message()
{
    static $errors = array
    ( JSON_ERROR_NONE           => null
    , JSON_ERROR_DEPTH          => 'Maximum stack depth exceeded'
    , JSON_ERROR_STATE_MISMATCH => 'Underflow or the modes mismatch'
    , JSON_ERROR_CTRL_CHAR      => 'Unexpected control character found'
    , JSON_ERROR_SYNTAX         => 'Syntax error, malformed JSON'
    , JSON_ERROR_UTF8           => 'Malformed UTF-8 characters, possibly incorrectly encoded'
    )
    ;
    // ADD THESE AT PHP 5.5+ OR USE json_last_error_msg()
    if (defined('JSON_ERROR_RECURSION'))        $errors[JSON_ERROR_RECURSION]        = 'One or more recursive references in the value to be encoded';
    if (defined('JSON_ERROR_INF_OR_NAN'))       $errors[JSON_ERROR_INF_OR_NAN]       = 'One or more NAN or INF values in the value to be encoded';
    if (defined('JSON_ERROR_UNSUPPORTED_TYPE')) $errors[JSON_ERROR_UNSUPPORTED_TYPE] = 'A value of a type that cannot be encoded was given';

    $error = json_last_error();
    return array_key_exists($error, $errors)
    ? $errors[$error]
    : "Unknown error ({$error})"
    ;
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hieloCommented:
You have a missing comma at the end of the "CustomerID" line, which makes the data useless.  if that is how you got it, then it needs to be fixed on their end.
Other than that, it works fine:
$string='{"EventType":10,
"EventSubType":1001,
"UnitId":91957012,
"CustomerID": 1994043,
"DeviceId":127,
"EventDateUtc":"2015-03-10T20:51:18.1390759Z",
"EventDesc":"System Closed by John Doe",
"UserDesc":"John Doe",
"ContactId":10724307,
"GroupId":11}';

$json_a = json_decode($string);
var_dump($json_a);

Open in new window

rwinnickAuthor Commented:
Hi Ray -
You're always there when I need you :-)
I gave 100 pts to Hielo even though you got there first.
I appreciate Hielo taking the time to help and felt he should have some reward.
I have sent an email to the third party pointing out their booo booo.
Thanks again for the insight, every time I use experts-exchange I learn a bit more.
From now on I will double check the input before second guessing the code.
Thanks guys
Richard
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Ray PaseurCommented:
Thanks for the points -- it's a really good question because it shows how subtle differences in an input string can completely change the behaviors.  FWIW, I'm always a fan of data visualization, and this one is hard to catch even when you can see the data!
hieloCommented:
Verify that they are in fact sending invalid data first, since you stated:
>> If I put quotes around the values I get correct results -
which tells me that you were "tampering" with that data sample.

On another note, numeric values don't require quotes around them.
Ray PaseurCommented:
Always glad to share points with @hielo, because I know when he comes to answer, he comes with experience and wisdom, and he's exactly right about JSON numeric values.  Douglas Crockford is the curator of the JSON standard.  It's maintained in a web site here:
http://json.org/

IMHO json.org is a web page worth studying for a few minutes.  JSON has virtually replaced XML as a data transport mechanism because it is lighter weight and more directly usable in client-side applications, such as AngularJS.

All of JSON is UTF-8.  That means (unfortunately) that someone will someday send you incorrectly encoded characters and you will need to find the UTF-8 collisions.  This link has a tool to speed the process:
http://iconoun.com/articles/collisions/#detecting_json_errors

Best to all, ~Ray
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.