I am trying to dynamically create a JSON object in the body of a HTTP request. The API I am calling does not like it when I include a field with a value set to an empty string " ".
Take name for example, if this is empty then the API will throw an error. What I want is to replace the Name dynamic variable and replace with a custom expression which only includes the "name" : dyanamic_variable, when Name is not equal to " ".

I have tried this :
if(equals(items('Apply_to_each_2')['name'], " "), "", ',"name": items('Apply_to_each_2')['name'],')
EDIT for clarification:
If name is not empty JSON looks like this...
{
"name": dynamic_variable,
"custom_fields" : {
"ContactID": dynamic_variable,
"AccountNumber": dynamic_variable,
"CustomerGrade": dynamic_variable,
"EnhancedGrade": dynamic_variable,
"BranchName": dynamic_variable,
"County": dynamic_variable,
"OuterPostcode": dynamic_variable,
}
}
where as if name is an empty string like so " ", then the JSON looks like this:
{
"custom_fields" : {
"ContactID": dynamic_variable,
"AccountNumber": dynamic_variable,
"CustomerGrade": dynamic_variable,
"EnhancedGrade": dynamic_variable,
"BranchName": dynamic_variable,
"County": dynamic_variable,
"OuterPostcode": dynamic_variable,
}
}
Thanks!