0

I would like to extract JSON ouput using PHP and tried below code but I'm getting output of single character instead of row wise values. I read similar posts but failed to get key pair values and getting output as single character. Wherever field values won't present, null is ok for that. How can I get output line by line for respective keys?

Tried code:

while (!feof($resultFile)) {
    $line = fgets ($resultFile);
    echo $line;
    $someArray = json_decode($line);
    foreach ($someArray as $key => $value) {
        echo $value["key"] . ", " . $value["status"] . "<br>";
  }
}

someArray output:

Array ( [key] => XYZ-6680 [status] => Open [components] => API [currentVersion] => Release1.2 [expectedVersion] => Array ( ) [customerInfo] => Default Calendar when the delegate responds to those invitations. ) Array ( [key] => XYZ-3325 [status] => Closed [components] => API [currentVersion] => Release 1.0 [expectedVersion] => Array ( [0] => Array ( [self] => https://bug.restify.com/rest/api/2/version/27771 [id] => 27771 [name] => Release1.2 [archived] => [released] => ) ) [customerInfo] => Fixed a number of bugs related to DA: * CrossMailboxSearch: Group label was shown even when all setting items of the group were hidden on New Account dialog ([https://bug.goog.restify/show_bug.cgi?id=1542 Bug 1542]) * After performing a bulk migration, the Delegated Admin user encountered an `HTTP Error 403` when attempting to download the list of provisioned accounts ([https://bug.goog.restify/show_bug.cgi?id=1039 Bug 1039]) ) Array ( [key] => XYZ-223 [status] => Closed [components] => API [currentVersion] => Release 1.3 [expectedVersion] => Array ( [0] => Array ( [self] => https://bug.restify.com/rest/api/2/version/29171 [id] => 29171 [name] => Release1.2 [archived] => [released] => ) ) [customerInfo] => "Default Calendar" user preference, `zimbraPrefDefaultCalendarId`. ) 

line output:

{"key":"XYZ-6680","status":"Open","components":"API","currentVersion":"Release1.2","expectedVersion":[],"customerInfo":"Default Calendar when the delegate responds to those invitations."} X, X
O, O
A, A
R, R
,
D, D
{"key":"XYZ-3325","status":"Closed","components":"API","currentVersion":"Release 1.0","expectedVersion":[{"self":"https://bug.restify.com/rest/api/2/version/27771","id":"27771","name":"Release1.2","archived":false,"released":false}],"customerInfo":"Fixed a number of bugs related to DA: * CrossMailboxSearch: Group label was shown even when all setting items of the group were hidden on New Account dialog ([https://bug.goog.restify/show_bug.cgi?id=1542 Bug 1542]) * After performing a bulk migration, the Delegated Admin user encountered an `HTTP Error 403` when attempting to download the list of provisioned accounts ([https://bug.goog.restify/show_bug.cgi?id=1039 Bug 1039])"} X, X
C, C
A, A
R, R
,
F, F
{"key":"XYZ-223","status":"Closed","components":"API","currentVersion":"Release 1.3","expectedVersion":[{"self":"https://bug.restify.com/rest/api/2/version/29171","id":"29171","name":"Release1.2","archived":false,"released":false}],"customerInfo":"\"Default Calendar\" user preference, `zimbraPrefDefaultCalendarId`."}X, X
C, C
A, A
R, R
,
", "

JSON (resultFile content):

{"key":"XYZ-6680","status":"Open","components":"API","currentVersion":"Release1.2","expectedVersion":[],"customerInfo":"Default Calendar when the delegate responds to those invitations."}
{"key":"XYZ-3325","status":"Closed","components":"API","currentVersion":"Release 1.0","expectedVersion":[{"self":"https://bug.restify.com/rest/api/2/version/27771","id":"27771","name":"Release1.2","archived":false,"released":false}],"customerInfo":"Fixed a number of bugs related to DA: * CrossMailboxSearch: Group label was shown even when all setting items of the group were hidden on New Account dialog ([https://bug.goog.restify/show_bug.cgi?id=1542 Bug 1542]) * After performing a bulk migration, the Delegated Admin user encountered an `HTTP Error 403` when attempting to download the list of provisioned accounts ([https://bug.goog.restify/show_bug.cgi?id=1039 Bug 1039])"}
{"key":"XYZ-223","status":"Closed","components":"API","currentVersion":"Release 1.3","expectedVersion":[{"self":"https://bug.restify.com/rest/api/2/version/29171","id":"29171","name":"Release1.2","archived":false,"released":false}],"customerInfo":"\"Default Calendar\" user preference, `zimbraPrefDefaultCalendarId`."}

Expected output:

Line by line values of key, status, components, currentVersion, expectedVersion, customerInfo.

Actual output:

Output

halfer
  • 19,824
  • 17
  • 99
  • 186
Jitesh Sojitra
  • 3,655
  • 7
  • 27
  • 46

3 Answers3

3

First of all, @Curious_mind is right about forcing output of json_decode as an associative array with second parameter to true. Then I think you should get what you want by echoing directly $key and $value, like so:

while (!feof($resultFile)) {
    $line = fgets ($resultFile);
    echo $line;
    $someArray = json_decode($line,true); 
    foreach ($someArray as $key => $value) {
        echo key . ", " . $value . "<br/>";
  }
}

BUT, careful, if $value is an array, you will get an error (can't just echo an array), so you need to handle the array resulting of you json recusivly.

I adapt the function found here: Echo a multidimensional array in PHP and added some testing to display string for boolean value too.

It should display the json values as you wish:

while (!feof($resultFile)) {
    $line = fgets ($resultFile);
    //echo $line;
    $someArray = json_decode($line,true); 
    RecursiveWrite($someArray);
}

function RecursiveWrite($array) {
    foreach ($array as $key => $value) {

        echo $key .', ';

        if(is_array($value)) {
            echo "<br>";
            RecursiveWrite($value);
        }
        elseif(is_bool($value)) {
            echo ($value? 'true' : 'false') . "<br>"; 
        }
        else {
            echo $value . "<br>";
        }
    }
}
Dexter0015
  • 1,029
  • 9
  • 14
1

How about that $someArray = json_decode($line,true);? because without the 2nd parameter true, json_decode() will return results as object and for that case you've to use $value->key while accessing keys not $value['key']

while (!feof($resultFile)) {
    $line = fgets ($resultFile);
    echo $line;
    $someArray = json_decode($line,true); # see the tweak here
    foreach ($someArray as $key => $value) {
        echo $value["key"] . ", " . $value["status"] . "<br/>";
  }
}
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
1

First I agree with Dexter0015's answer. It should be marked as the correct answer as it will deal with a wide variety of results.

I just thought I'd throw my two cents in for a shorter and very problem specific to the user's issue.

/* Load up a record that is json encoded */
$json = '{"key":"XYZ-6680","status":"Open","components":"API","currentVersion":"Release1.2","expectedVersion":[],"customerInfo":"Default Calendar when the delegate responds to those invitations."}';
/* Use json_decode to convert the JSON string to a PHP Array. Be sure and set the second parameter to true in json_decode to get an array and not an object */
$array = json_decode($json, true);

/*
 * Now iterate through the result extracting the key and value of the array created from json decode 
 * There could be instances (expectedVersion) that may have an array of values. So test to see
 * if the array value is an array. If so using print_r, otherwise just echo out the $value as a string
 */
foreach ($array as $key => $value) {
    if (!is_array($value)) {
        echo '* Key ' . $key . ' has a value of :' . $value . PHP_EOL;
    } else {
        echo "* Key " . $key . ' has an array of values :' . print_r($value, true) . PHP_EOL;
    }
}
Jim_M
  • 273
  • 1
  • 2
  • 10
  • For more information regarding json_decode you can go here : http://php.net/manual/en/function.json-decode.php – Jim_M Feb 01 '19 at 18:24