Json_encode returns unusable result for updating phone number

I’m trying to update a contact from a website that uses the current InfusedWOO plugin v4.4.5, and specifically trying to update a contact’s phone number. Problem: somewhere along the way, the update data is getting formatted wrong, so the API is rejecting it.

I start with an update variable created like so:

$update_data = ['given_name' => $given_name,'family_name' => $family_name];


The API developer’s guide says that phone numbers should be a nested array, so if there is a phone number, I add it to $update_data:

$update_data['phone_numbers'] = ['number' => $formatted_phonenum,'field' => 'PHONE1'];

Then I use $update_data in a call to the plugin’s function iw_keap_restV1Request():

$update_result = iw_keap_restV1Request("contacts/{$matching_contact_id}", 'PATCH', json_encode($update_data));

But it doesn’t work. If I dump the contents of $update_data using json_encode, I get this:

{“given_name”:“John”,“family_name”:“Doe”,“phone_numbers”:{“number”:“5551011110”,“field”:“PHONE1”}}

However, by experimenting with Postman, I found that what the API wants to see is this:

{“given_name”:“John”,“family_name”:“Doe”,“phone_numbers”:[ {“number”:“5551011110”,“field”:“PHONE1”} ] }

with the phone_numbers sub-array contained inside both square brackets and curly braces.

Why is this happening, and is there a way to get $update_data into the format that the API wants without building the JSON string myself?

Hi @Sean_Manning,

The fix is to change how phone_numbers is structured before it gets encoded.

Right now it’s being added as a single associative array:

$update_data['phone_numbers'] = ['number' => $formatted_phonenum,'field' => 'PHONE1'];

That structure will always encode as a JSON object, but the endpoint expects phone_numbers to be an array of entries, so you just need one extra nesting level:


$update_data['phone_numbers'] = [
    [
        'number' => $formatted_phonenum,
        'field'  => 'PHONE1'
    ]
];

That way json_encode() produces the correct array format automatically, and you can keep using iw_keap_restV1Request() as-is without building JSON manually.

Please let me know if that resolves it. Happy to help.

That fixed it. Thanks!