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!

2
  • 1
    stackoverflow.com/questions/3488425/… may be worth a look for the URL problem. Commented May 31, 2018 at 13:26
  • 1
    learn to read json. If you dont have a proper IDE, you can always copy-paste it at 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. Commented May 31, 2018 at 13:32

2 Answers 2

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.

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

2 Comments

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 api.lever.co/v0/postings/… ?
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.
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>

1 Comment

Not sure about using $characters['0'] inside the loop.

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.