Link to home
Start Free TrialLog in
Avatar of fixitben
fixitben

asked on

PHP json decode

HI

I am trying to pull in some json data into php to manipulate. When I do it nothing is displayed. If I break it down into one section it will import, but I need all the sections.

/*JSON:sessions*/ { "index":"1", "sid":"0x00000001", "acc":"00000001", "vendorid":"47331", "haspid":"2146096202", "haspname":"2146096202", "prid":"0", "productname":"", "prv":"Local", "fid":"0", "fn":"", "cli":"172.16.12.19", "usr":"USERID", "scrn":"2", "mch":"USERID-D", "pid":"6452", "seats":"1", "lt":"Tue Dec 6, 16:49:07", "rt":"11:56:18", "lm_version":"17000", "lm_version_full":"17.0", "lm_build":"17.0.1.48292", "api_version":"7.03", "ddis":"0" }, { "index":"2", "sid":"0x00000002", "acc":"00000002", "vendorid":"47331", "haspid":"2146096202", "haspname":"2146096202", "prid":"0", "productname":"", "prv":"Local", "fid":"0", "fn":"", "cli":"172.16.12.19", "usr":"USERID", "scrn":"2", "mch":"USERID-D", "pid":"6452", "seats":"1", "lt":"Tue Dec 6, 16:50:40", "rt":"11:46:21", "lm_version":"17000", "lm_version_full":"17.0", "lm_build":"17.0.1.48292", "api_version":"7.03", "ddis":"0" }, { "fhaspid":"0", "fprod":"0", "ffea":"0", "cnt":"2" } /* 0 SNTL_ADMIN_STATUS_OK */  

Open in new window



This is what I have and doesn't work. I really haven't messed with json very much with PHP so this is all new.

$jsondata = file_get_contents($json_string);
//$jsondata = '{ "index":"1", "sid":"0x00000001", "acc":"00000001", "vendorid":"47331", "haspid":"2146096202", "haspname":"2146096202", "prid":"0", "productname":"", "prv":"Local", "fid":"0", "fn":"", "cli":"172.16.12.19", "usr":"USERID", "scrn":"2", "mch":"USERID-D", "pid":"6452", "seats":"1", "lt":"Tue Dec 6, 16:49:07", "rt":"11:58:38", "lm_version":"17000", "lm_version_full":"17.0", "lm_build":"17.0.1.48292", "api_version":"7.03", "ddis":"0" }, { "index":"2", "sid":"0x00000002", "acc":"00000002", "vendorid":"47331", "haspid":"2146096202", "haspname":"2146096202", "prid":"0", "productname":"", "prv":"Local", "fid":"0", "fn":"", "cli":"172.16.12.19", "usr":"USERID", "scrn":"2", "mch":"USERID-D", "pid":"6452", "seats":"1", "lt":"Tue Dec 6, 16:50:40", "rt":"11:48:41", "lm_version":"17000", "lm_version_full":"17.0", "lm_build":"17.0.1.48292", "api_version":"7.03", "ddis":"0" }, { "fhaspid":"0", "fprod":"0", "ffea":"0", "cnt":"2" }';
echo $jsondata;
$obj = json_decode($jsondata, true);
print_r($obj);

Open in new window



Any help is appreciated. Thanks
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
As Chris stated above, your JSON is not valid.  An easy way to validate JSON (and see how it should be broken down) is to paste your string into an online JSON parser.  Just google for "JSON parser" -- there are a ton of them out there.

Once you have your JSON fixed, that might resolve the problem you're encountering.
This may give some insights into how JSON works with PHP.
https://www.experts-exchange.com/articles/22519/Understanding-JSON-in-PHP-and-JavaScript-Applications.html

See the part about "Dealing with JSON_Decode() Errors"
to follow Chris Sanyon, check if you get something with the following :
$jsondata = "[" . file_get_contents($json_string) . "]";
$obj = json_decode($jsondata, true);
print_r($obj);

Open in new window

If you are looking for an online JSON formatter / validator I have this one pinned
https://jsonformatter.curiousconcept.com/

Useful for validating and formatting JSON as well as locating and fixing errors.
Avatar of fixitben
fixitben

ASKER

Ok I added the [ ] brackets and it just gives the error
Parse error: syntax error, unexpected T_VARIABLE in /var/www/html/compress.php on line.
Which is kind of useless. I also tried doing it like leakim971, but it was the same error.

I ran it through the json validator and it validates with all three json standards on this page. https://jsonformatter.curiousconcept.com/ 

Any more ideas.


$jsonstring = '[{ "index":"1", "sid":"0x00000001", "acc":"00000001", "vendorid":"47331", "haspid":"2146096202", "haspname":"2146096202", "prid":"0", "productname":"", "prv":"Local", "fid":"0", "fn":"", "cli":"172.16.12.19", "usr":"USERID", "scrn":"2", "mch":"USERID-D", "pid":"6452", "seats":"1", "lt":"Tue Dec 6, 16:49:07", "rt":"11:58:38", "lm_version":"17000", "lm_version_full":"17.0", "lm_build":"17.0.1.48292", "api_version":"7.03", "ddis":"0" }, { "index":"2", "sid":"0x00000002", "acc":"00000002", "vendorid":"47331", "haspid":"2146096202", "haspname":"2146096202", "prid":"0", "productname":"", "prv":"Local", "fid":"0", "fn":"", "cli":"172.16.12.19", "usr":"USERID", "scrn":"2", "mch":"USERID-D", "pid":"6452", "seats":"1", "lt":"Tue Dec 6, 16:50:40", "rt":"11:48:41", "lm_version":"17000", "lm_version_full":"17.0", "lm_build":"17.0.1.48292", "api_version":"7.03", "ddis":"0" }, { "fhaspid":"0", "fprod":"0", "ffea":"0", "cnt":"2" }]'
//$jsondata = "[" . file_get_contents($json_string) . "]";
$obj = json_decode($jsonstring, true);
print_r($obj);

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This seems to test out OK.  Hopefully the comments will shed some light on what we're trying to do.
https://iconoun.com/demo/temp_fixitben.php
<?php // demo/temp_fixitben.php
/**
 * https://www.experts-exchange.com/questions/28987794/PHP-json-decode.html
 *
 * New to PHP?
 * https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html
 *
 * Learn About JSON in PHP?
 * https://www.experts-exchange.com/articles/22519/Understanding-JSON-in-PHP-and-JavaScript-Applications.html
 */
error_reporting(E_ALL);
echo '<pre>';

// TEST DATA FROM THE POST AT E-E IS INVALID JSON
$jsondata = '{ "index":"1", "sid":"0x00000001", "acc":"00000001", "vendorid":"47331", "haspid":"2146096202", "haspname":"2146096202", "prid":"0", "productname":"", "prv":"Local", "fid":"0", "fn":"", "cli":"172.16.12.19", "usr":"USERID", "scrn":"2", "mch":"USERID-D", "pid":"6452", "seats":"1", "lt":"Tue Dec 6, 16:49:07", "rt":"11:58:38", "lm_version":"17000", "lm_version_full":"17.0", "lm_build":"17.0.1.48292", "api_version":"7.03", "ddis":"0" }, { "index":"2", "sid":"0x00000002", "acc":"00000002", "vendorid":"47331", "haspid":"2146096202", "haspname":"2146096202", "prid":"0", "productname":"", "prv":"Local", "fid":"0", "fn":"", "cli":"172.16.12.19", "usr":"USERID", "scrn":"2", "mch":"USERID-D", "pid":"6452", "seats":"1", "lt":"Tue Dec 6, 16:50:40", "rt":"11:48:41", "lm_version":"17000", "lm_version_full":"17.0", "lm_build":"17.0.1.48292", "api_version":"7.03", "ddis":"0" }, { "fhaspid":"0", "fprod":"0", "ffea":"0", "cnt":"2" }';

// MAKE IT VALID - AN ARRAY REPRESENTATION
$validjson = '[' . $jsondata . ']';

// DECODE THE JSON AND TEST FOR SUCCESS OR FAILURE
$arr = json_decode($validjson);
if (!$arr) trigger_error( json_last_error_message(), E_USER_ERROR);

// ACTIVATE THIS TO SEE THE DECODED JSON
// print_r($arr);

// GET THE LAST ELEMENT
$x = array_pop($arr);
$cnt = $x->cnt;
echo PHP_EOL . "cnt = $cnt";
echo PHP_EOL;

// ITERATE OVER THE REMAINING ELEMENTS
foreach ($arr as $x)
{
    echo PHP_EOL . "acc = $x->acc";
    echo PHP_EOL . "lt  = $x->lt";
    echo PHP_EOL;
}


// FUNCTION FOR ERROR VISUALIZATION
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

If you're new to PHP and want to find some good learning resources, this article can help.  Just skip over the things you already know from coursework or experience.
https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html
Thanks for your help guys. I feel dumb on that one. I knew about the [ ] thing, but when it didn't work I gave up. It was the semi colon at the end of the line that did it. Thanks again.