0

I have a JSON feed which I am parsing via PHP. I am having issues getting some nested elements to echo which I would appreciate some assistance on.. I've looked at loads of related posts here but cant seem to get the logic to work on my specific JSON feed. Could someone advise what I am doing wrong?

JSON feed is here > https://api.lever.co/v0/postings/leverdemo?skip=1&limit=3&mode=json

The elements, I am struggling to parse are the "categories" parent and child nodes of "team", "location" and "commitment".

I was thinking this would work - but it does not...

<?php

$url = 'feed.json'; 
$data = file_get_contents($url); 
$characters = json_decode($data, true);

?>

<table>
<tbody>
    <tr>
        <th>Job title</th>
        <th>Team</th>
        <th>Location</th>
        <th>Commitment</th>
        <th>DescriptionPlain</th>
        <th>applyUrl</th>
    </tr>
    <?php foreach ($characters as $character) : ?>
    <tr>
        <td> <?php echo $character['text']; ?> </td>



        <td> <?php echo $character['categories'][2]['team'] ?></td>
        <td> <?php echo $character['categories'][2]->team ?></td>
        <td> <?php echo $character['categories'][1]->location ?></td>
        <td> <?php echo $character['categories'][0]->commitment ?></td>

        <td> <?php echo $character['descriptionPlain']; ?> </td>
        <td> <?php echo $character['applyUrl']; ?> </td>

    </tr>
    <?php endforeach; ?>
</tbody>

Note, its just the categories children that fail to echo? Also noticed that if I use the full url in the $url variable it all fails? But from local it works??

Any ideas??? Thanks!

dubbs
  • 1,167
  • 2
  • 13
  • 34
  • 1
    https://stackoverflow.com/questions/3488425/php-ini-file-get-contents-external-url may be worth a look for the URL problem. – Nigel Ren May 31 '18 at 13:26
  • 1
    learn to read json. If you dont have a proper IDE, you can always copy-paste it at **[jsonlint.com](https://jsonlint.com)**, press validate, and the site will format it for you. You would have seen that `categories` is not an array , but an embedded object. – YvesLeBorg May 31 '18 at 13:32

2 Answers2

2

It should be:

    <td> <?php echo $character['categories']["team"] ?></td>
    <td> <?php echo $character['categories']["location"] ?></td>
    <td> <?php echo $character['categories']["commitment"] ?></td>

instead. The numeric keys are not present on the array in the data. Also "categories" is not an object, so you cannot use the arrow (->) notation.

cjs1978
  • 477
  • 3
  • 7
  • P.S. Do you know why this works when calling a local json object from same folder, but not when I call it from url at https://api.lever.co/v0/postings/leverdemo?skip=1&limit=3&mode=json ? – dubbs May 31 '18 at 13:29
  • I think Nigel Ren's comment above is correct. Are you working on your local machine (with XAMPP or the like) - then you can change the appropriate setting in php.ini. – cjs1978 May 31 '18 at 13:33
0

EDIT

You get an error because you are trying to access an object, actually you have an array, here is the solution hope it helps :

<?php
$url = 'https://api.lever.co/v0/postings/leverdemo?skip=1&limit=3&mode=json'; 
$data = file_get_contents($url); 
$characters = json_decode($data, true);
$nb = count($characters);
?>
<table>
<tbody>
    <tr>
        <th>Job title</th>
        <th>Team</th>
        <th>Location</th>
        <th>Commitment</th>
        <th>DescriptionPlain</th>
        <th>applyUrl</th>
    </tr>
    <?php while($nb > 0){ 
    $nb--;
    $nb_lists = count($characters[$nb]['lists']);
    ?>
    <tr>
    <?php 
        while($nb_lists > 0){
            $nb_lists--;
            ?>
        <td> <?php if(isset($characters[$nb]['lists'][$nb_lists]['text'])){ echo $characters[$nb]['lists'][$nb_lists]['text'];} ?> </td>
        <?php } ?>
        <td> <?php if(isset($characters[$nb]['categories']['team'])){echo $characters[$nb]['categories']['team'];} ?></td>
        <td> <?php if(isset($characters[$nb]['categories']['team'])){echo $characters[$nb]['categories']['team'];} ?></td>
        <td> <?php if(isset($characters[$nb]['categories']['location'])) {echo $characters[$nb]['categories']['location'];} ?></td>
        <td> <?php if(isset($characters[$nb]['categories']['commitment'])){ echo $characters[$nb]['categories']['commitment'];} ?></td>
        <td> <?php if(isset($characters[$nb]['descriptionPlain'])){echo $characters[$nb]['descriptionPlain']; }?> </td>
        <td> <?php if(isset($characters[$nb]['applyUrl'])){echo $characters[$nb]['applyUrl'];} ?> </td>
    </tr>
    <?php } ?>
</tbody>
executable
  • 3,365
  • 6
  • 24
  • 52