Home > database >  Format array of objects for possible json output for Postman/GET requests
Format array of objects for possible json output for Postman/GET requests

Time:03-13

So I can't seem to figure this out, so I'm reaching out to see if someone might be able to help me.

Please let me know what the best output is so that I could use GET to retrieve clean data for the endpoint that I've created.

I've tried to include as many details as possible if it makes sense, so please let me know if I'm missing something before downvoting.

I have the following method:

function instagram_posts(): bool|string
{
    if (!function_exists('is_plugin_active')) {
        include_once(ABSPATH . 'wp-admin/includes/plugin.php');
    }
    if (!is_plugin_active('fh-instagram/autoload.php')) {
        return false;
    }
    if (empty($items = Instagram::get_items_for_api(50))) {
        return false;
    }
    var_dump($items);

    var_dump(json_encode($items));

    return json_encode($items);
}

var_dump($items); gives me the following output:

array(50) {
  [0]=>
  object(Plugin\Instagram\Item)#976 (7) {
    ["id":"Plugin\Instagram\Item":private]=>
  }
  [1]=>
  object(Plugin\Instagram\Item)#1030 (7) {
    ["id":"Plugin\Instagram\Item":private]=>
    string(17) "17842233125750202"
  }
}

When I run var_dump(json_encode($items)); I get the following output:

string(151) "[{},{}]"

How can I convert my array of objects so that it can transform it to json and then use it within Postman? This is what it currently looks like in Postman:

array(50) {
  [0]=>
  object(Plugin\Instagram\Item)#973 (7) {
    ["id":"Plugin\Instagram\Item":private]=>
    string(17) "17992874035441353"
  }
  [1]=>
  object(Plugin\Instagram\Item)#1027 (7) {
    ["id":"Plugin\Instagram\Item":private]=>
    string(17) "17842233125750202"
  }
}

It should be outputted such as:

[
  {"id": etc..}
]

All help will be appreciated!

The instagram_posts method is being use below:

add_action('rest_api_init', function () {
    register_rest_route( 'instagram', '/posts/', [
        'methods'  => 'GET',
        'callback' => 'instagram_posts',
    ]);
});

So I can use Postman to access the endpoint: http://headlesscms.com.local/wp-json/instagram/posts

CodePudding user response:

Since the property you want is private, it won't be included in the results of json_encode(). Only public properties will.

You need to create a multidimensional array with the structure you want and encode that.

// This is the new array we will push the sub arrays into
$results = [];

foreach($items as $item) {
    $results[] = ['id' => $item->get_id()];
}

return json_encode($results);

This will give you a json structure that looks like:

[
    {
        "id": 1
    },
    {
        "id": 2
    },
    ...
]

Alternative format

If you only want a list of id's, you might not need to create a multidimensional array, but rather just return a list of ids.

In that case, do this:

$results = [];

foreach ($items as $item) {
    $results[] = $item->get_id();
}

return json_encode($results);

That would give you:

[
    1,
    2,
    ...
]
  • Related