Format array of objects for possible json output for Postman/GET requests - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Sunday, March 13, 2022

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

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!



source https://stackoverflow.com/questions/71452085/format-array-of-objects-for-possible-json-output-for-postman-get-requests

No comments:

Post a Comment