Me job
Me job

Reputation: 11

How can I choose only specific fields for mailchimp lists members for results

When I tried to get all the members from a list, I received more data then I needed.

I want to minimize the return json for only 4 fields, but it's seems that the 'fields' parameter doesn’t really do something.

Here my code:

$list_id = $this->_job->getListId();
$list =  $this->_MailChimp_obj->get('lists/' . $list_id . '/members', array(
    'fields' => array(
        'id',
        'email_address',
        'subscribed',
        'merge_fields'
    ),
    'count' => 50,
    'offset' => 0
));

I use this API: https://github.com/drewm/mailchimp-api

Thanks!

Upvotes: 1

Views: 775

Answers (2)

Hamza Tasneem
Hamza Tasneem

Reputation: 92

For anyone coming in late, you have to prepend "members." at start of each individual field (element of array here), as there is no clear example given in the API Reference, the above code would look like

$list_id = $this->_job->getListId();
$list =  $this->_MailChimp_obj->get('lists/' . $list_id . '/members', array(
    'fields' => array(
        'members.id',
        'members.email_address',
        'members.subscribed',
        'members.merge_fields' //members.merge_fields.any_subfield_name
    ),
    'count' => 50,
    'offset' => 0
));

Furthermore if you are using Node.js with MailChimp marketing API, the code would be:

const mailchimp = require("@mailchimp/mailchimp_marketing");

mailchimp.setConfig({
  apiKey: "your api key",
  server: "your server prefix",
});

const listId = "your list id";
const offset = 0;
const count = 10;

const run = async () => {
  const response = await mailchimp.lists.getListMembersInfo(listId, {
    offset,
    count,
    fields: ['members.id','members.email_address','members.subscribed','members.merge_fields'],
  });

  console.log(response);
  
};

run();

Upvotes: 3

Tuinstoelen
Tuinstoelen

Reputation: 1221

You need to prepend the fields with "member.". If you take a look at the raw JSON output, it is more clear why - the items are returned in an enclosing "members" item.

Upvotes: 1

Related Questions