3

I have to write a script to read a JSON file which will have information about a Data Pipeline - mainly three components - Status, Comments and Timestamp. Once I read the file in JSON, I need to print out the output for that respective Data Pipeline with all the three components. The output would look something like this as below:

Name: Apollo Status: Pending Comments: Monthly report Timestamp: 00:00

I have the following script to generate the JSON file:

<?php
    $data = array(
             "name"=>"Apollo", 
             "cob"=> array(
                          status=> "completed",
                          comment=> "Monthly report",
                          timestamp=> "00:00"
                         ),
              );

header('Content-Type: application/json');
echo json_encode($data);
?>

I have the following script to read the JSON file generated and to print the above required output:

<?php
    $data = file_get_contents ('./cob_details.json');
    $json = json_decode($data, true);

    echo ('<pre>');
    print_r ($json);
    echo ('</pre>');

    echo ('<br>output:</br>');

    foreach ($json as $key => $value)
    {
    echo "Name: $value Status: $value]<br />";
    }
?>

I am particularly new to JSON and PHP....Can you please let me know what am I missing over here in this script to get the required output or where am I going wrong?

1
  • 1
    What is actually the problem you encounter? Do you get an error? Commented Oct 2, 2013 at 22:13

2 Answers 2

4

I think the problem is that your json data generator has a minor error

Try this instead, field names in an array have to be text literals and you forgot to wrap the names in quotes

<?php
    $data = array(
             "name" => "Apollo",
             "cob"  => array(
                          'status'    => "completed",
                          'comment'   => "Monthly report",
                          'timestamp' => "00:00"
                         )
              );

    echo json_encode($data);
?>

Now the code that read the data needs to loop over the first array and then grab the bits it is interested in from the inner array using the $value variable which is the address of the inner array. I am assuming the actual data contains more fields than your sample code so I used a switch but an if would do if its not that complicated

<?php
    $data = file_get_contents ('./cob_details.json');
    $json = json_decode($data, TRUE);

    echo ('<pre> print the json ');
    print_r ($json);
    echo ('</pre>');

    echo '<br>output:</br>';

    foreach ($json as $key => $value)
    {
        switch ( $key ) {
            case 'name' :
                echo "Name: $value";
                break;
            case 'cob' : 
                echo ' Status: ' . $value['status'] . ']<br />';
                break;
            case 'another field' :
                // and so on
                break;
        }

}
Sign up to request clarification or add additional context in comments.

Comments

2

Try:

$data = file_get_contents ('./cob_details.json');
$json = json_decode($data, true);
foreach ($json as $key => $value) {
    if (!is_array($value)) {
        echo $key . '=>' . $value . '<br/>';
    } else {
        foreach ($value as $key => $val) {
            echo $key . '=>' . $val . '<br/>';
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.