How to format MembershipRequest objects for an Add New Project request?

I’m trying to create a private project that assigns the members from a Trello card to that project, using a single Add New Project request. I’m using Zapier to connect Trello and Clockify. Everything is working except the membership assignments. I keep getting this error:

JSON parse error: Cannot deserialize value of type java.util.ArrayList<com.clockify.adapter.http.project.MembershipRequest> from String value (token JsonToken.VALUE_STRING) (HTTP Status Code: 400)

I’m looping through a list to collect the properties, and push the objects to a list, then inserting that list of objects into an array as the value the “memberships” property. My objects looks like this:

{
“userId”:“xxxxxxxxx”,
“hourlyRate”:“undefined”,
“costRate”:“undefined”,
“targetId”:“undefined”,
“membershipType”:“PROJECT”,
“membershipStatus”:“ACTIVE”
}

I’m a total novice, so I’m hoping I’m just missing some basic syntax here that’s making these objects unreadable to the API. Is it acceptable to have these properties undefined? Is that the correct way to notate undefined in JSON? How could I have the target ID (of the project) when the project isn’t yet made? I would be grateful for some suggested resources in lieu of a definite answer. :slight_smile:

Hello,

You could remove the rates, and the targetId, as the targetId is generated after a project has been created. Here’s how the JSON body could look like:

{
  "name": "My API Project",
  "memberships": [{
    "userId": "ENTER-USER-ID-HERE",
    "membershipType": "PROJECT",
    "membershipStatus": "ACTIVE"
  }]
}

The parameter userId must have a value if you are creating a private project and assigning a user to it. You can get all the userIds by using the Find all users on workspace call
Method: GET
URL: https://api.clockify.me/api/v1/workspaces/{workspaceId}/users

How are you planning on automating the addition of the userId? You could consider matching the userId in Clockify with users in Trello. A custom field comes to mind. You could then use that information to use in the “memberships” param.

Thanks for your response. I removed the extra pairs from the membershipRequests. Once I knew that was right I was able to troubleshoot and found that Zapier was wrapping each JSON value in double quotes, rendering the array to a string. I was able to build a custom HTTP request and manually format it to be correct. Thanks for your help!

To answer your question, I am looping through checklist items on a Trello card which contain Roles and Assignees for gigs. A table in Zapier translates the Trello ID to a Clockify ID. That’s used to create the list of Member request objects and task objects.

When the project is created, each user’s role on the project is made into a task which is pre assigned to them so they can easily clock hours on whatever projects they’re booked on. It’s working!

The error you’re encountering seems to indicate an issue with the JSON structure or data type mismatch when Clockify is attempting to deserialize the JSON payload. Here are a few suggestions that might help you troubleshoot the issue:

  1. Undefined Values:
  • Using the string “undefined” might be causing issues. In JSON, you generally use null to represent an undefined or absent value. Try using null instead of the string “undefined” for properties like “hourlyRate,” “costRate,” and “targetId.”
  1. JSON Array Structure:
  • Ensure that the “memberships” property is an array of objects. The structure should look like:

“memberships”: [
{
“userId”: “xxxxxxxxx”,
“hourlyRate”: null,
“costRate”: null,
“targetId”: null,
“membershipType”: “PROJECT”,
“membershipStatus”: “ACTIVE”
},
// Additional membership objects if needed
]

"memberships": [
  {
    "userId": "xxxxxxxxx",
    "hourlyRate": null,
    "costRate": null,
    "targetId": null,
    "membershipType": "PROJECT",
    "membershipStatus": "ACTIVE"
  },
  // Additional membership objects if needed
]
  1. Syntax Considerations:
  • Ensure that your JSON syntax is correct. Commas between objects in an array and proper nesting are crucial.
  1. Target ID:
  • If the “targetId” is required but the project isn’t created yet, you might need to make a separate API request to create the project first and then use its ID in the membership objects.

Here’s a basic example with some improvements based on the suggestions:

{
“name”: “Your Project Name”,
“memberships”: [
{
“userId”: “xxxxxxxxx”,
“hourlyRate”: null,
“costRate”: null,
“targetId”: null,
“membershipType”: “PROJECT”,
“membershipStatus”: “ACTIVE”
}
// Additional membership objects if needed
]
}

{
  "name": "Your Project Name",
  "memberships": [
    {
      "userId": "xxxxxxxxx",
      "hourlyRate": null,
      "costRate": null,
      "targetId": null,
      "membershipType": "PROJECT",
      "membershipStatus": "ACTIVE"
    }
    // Additional membership objects if needed
  ]
}

Remember to replace “Your Project Name” with the actual project name.