<<If:>><<Endif>> template condition add an extra comma to my JSON webhook

Hi,
I’m facing a very strange issue

When I use <<If:>><> condition in the body of a webhook, it adds an extra comma at the end

{
“id”: “<<[ID]>>”,
“EntrepriseID”: “<<[EntrepriseID]>>”,
“ContactID”: “<<[ContactID]>>”,
“nom_du_devis”: “<<[nom_du_devis]>>”,
“date_du_devis”: “<<[date]>>”
<<If:COUNT([Related CAD])>0>>,
“publicitesCAD”: [
<<Start: [Related CAD]>>{
“numero”: “<<[id].[numero]>>”,
“date”: “<<[id].[sortie]>>”,
“format”: “[id].[format]”
}
<>
]
<>
}

And here is my raw json :slight_smile: and do you see that ],} ?

“Payload”: “{“id”: “45fg25fg”,“EntrepriseID”: “8725583923”,“ContactID”: “590951”,“nom_du_devis”: “Plan média”,“date_du_devis”: “22/01/2024”,“publicitesCAD”: [{“numero”: “302”,“date”: “19/02/2024”,“format”: “3”},{“numero”: “305”,“date”: “21/05/2024”,“format”: “3”}],}”,

So my JSON is not well formatted and my bot doesn’t’ work

I’ve just run another test using a simplified expression

{
“id”: “<<[ID]>>”,
<<If:COUNT([Related CAD])>0>>
“publicitesCAD”: “OK”
<>
}

And here is the JSON output. Do you see the comma ",}

“Payload”: “{“id”: “45fg25fg”,“publicitesCAD”: “OK”,}”

How do I remove this comma that’s preventing the JSON from being formatted correctly?

You can use SUBSTITUTE : SUBSTITUTE( <text to be changed>, “,}”, “}”)

Hi @WillowMobileSys ,
Yes, I’m familiar with the SUBSTITUTE function, but how do I use it in a template block for JSON output? How do I use it with these instructions?
{
“id”: “<<[ID]>>”,
“EntrepriseID”: “<<[EntrepriseID]>>”,
“ContactID”: “<<[ContactID]>>”,
“nom_du_devis”: “<<[nom_du_devis]>>”,
“date_du_devis”: “<<[date]>>”
<<If:COUNT([Related CAD])>0>>,
“publicitesCAD”: [
<<Start: [Related CAD]>>{
“numero”: “<<[id].[numero]>>”,
“date”: “<<[id].[sortie]>>”,
“format”: “[id].[format]”
}
<>
]
<>
}

Of course. Silly me. I misunderstood your second post.

Taking a closer look at your formatting, these are pre-processing templates so you still need the follow the rules just as if this was PDF doc generation template.

The IF needs to have an “endif” and the START needs to have an “end”. Links below to articles for examples.

***************************************
Unfortunately, after reading another thread (here) I think you might still have an issue UNLESS the problem of the extra column after an “endif” was resolved. You may want to try it out anyway.

There was this comment:

”Just FYI for anyone finding this thread, I raised the issue with Appsheet support, who gave the solution of replacing <><> with an Appsheet If() expression. This worked in my case of a webhook posting a JSON Payload to an external API, so just sharing here in case it helps others.”

It sounds like the person was able to use a REGULAR If expression instead of a template based one.
****************************************

Once you have that format sorted out, I understand that you are partly using the IF to decide if a comma is inserted or not. But I would suggest something more like this:


<<If:COUNT([Related CAD])=0>>
“date_du_devis”: “<<[date]>>”
<<endif>>
<<If:COUNT([Related CAD])>0>>
“date_du_devis”: “<<[date]>>”,
“publicitesCAD”: [
<<Start: [Related CAD]>>{
“numero”: “<<[id].[numero]>>”,
“date”: “<<[id].[sortie]>>”,
“format”: “[id].[format]”

<<End>>
<<EndIf>>

Use If expressions in templates

Use Start expressions in templates

I hope this helps better!!

1 Like

I saw your post in MultiTech’s thread. Your issue is a little different.

Did you by chance try implementing the IF formatted like an AppSheet IF expression instead of a template based IF? The older post I linked seemed to imply that fixed (worked around) the problem.

Ok, I make screenshots in order to better see my code

With << IF: >> condition in the JSON template

“Payload”: “{“id”: “45fg25fg”,“publicitesCAD”: [{“numero”: “302”},{“numero”: “305”},]}”

Output is NOT OK. Extra comma appears in },]}

Without << IF: >> condition in the JSON template

Output is OK. No extra comma in }]}

“Payload”: “{“id”: “45fg25fg”,“publicitesCAD”: [{“numero”: “302”},{“numero”: “305”}]}”,

CONCLUSION :

<< IF: >> templcondition adds an extra comma in the JSON output

Now, I understand that I could use the Appsheet If() condition in order to avoid that.

BUT, how ? How I make a loop in a IF() condition ? Can you help me please ?

Can you try this approach again but instead place the “}” on its own line adn then the “End” on the next line after that? I believe “End” ’s must be on a separate row unless it is in a table such as a Doc table.

Hi,
I place the } on its on line and the “end” on the next line after

“Payload”: “{“id”: “45fg25fg”,“publicitesCAD”: [{“numero”: “302”},{“numero”: “305”},]}”

No change. The extra comma is still there !

1 Like