In a workflow's JSON format, how can I use AppSheet's expression to include or exclude a JSON object? Currently, any value return by expression will be JSON stringify

Say, I want a part of JSON template to be included or excluded based on a condition and that part is a JSON object and not a simple string. Example:

<<IF(TRUE, '{"type": "text\ntext"}', '')>>

Currently, that would return a string with escaped text like
{\"type\": \"text\\ntext\"}

Note that above JSON is hand typing. So it might not have a valid format. Itโ€™s just to give you an idea.

And current workaround is create multiple workflows with different JSON template files but it is only good for templates with one or two condition checks.

One suggestion is to include a JSON type in AppSheetโ€™s column type list. Anything printed from the JSON type will not be escaped (or stringify).

Thanks.

Solved Solved
0 4 1,884
1 ACCEPTED SOLUTION

Unless I am completely misunderstanding your initial post, in your JSON body you can do things like:

{some JSON code}
<<If: (some condition in your table data)>>
{only sometimes JSON code}
<<EndIf>>
<<If: (a different condition in your table data)>>
{only sometimes JSON code}
<<EndIf>>
{more JSON code}

You donโ€™t have to use the IF() from within AppSheet. You could even have multiple different JSON templates inside one file depending on a certain value from your data.

My personal example (JSON sent to Discord):

View solution in original post

4 REPLIES 4

TyAlevizos
Participant V

Interesting challenge. You may want to consider putting your snippets into fields and/or columns in a data source, then, just refer to them by โ€œnameโ€, e.g.

<<IF(Contains([Name],"ALPHA"),[Pattern1],[Pattern2])>>

(pattern1 and pattern2 are two different JSON snippets, [Name] is just a field I am keying off of)

Hereโ€™s a working example. Is this the kind of thing you were asking about? Or something else perhapsโ€ฆ

https://www.appsheet.com/samples/Built-from-your-app-spec-?appGuidString=3c410dc5-5eca-4190-93f9-217...

If you use [Pattern1] in a body template like in

Say [Pattern1] is { x: 5, y: 6 }
All those snippets will be stringify like this
JSON.stringify({ x: 5, y: 6 })
And it will be printed out like
"{"x":5,"y":6}" which is a string.

Unless I am completely misunderstanding your initial post, in your JSON body you can do things like:

{some JSON code}
<<If: (some condition in your table data)>>
{only sometimes JSON code}
<<EndIf>>
<<If: (a different condition in your table data)>>
{only sometimes JSON code}
<<EndIf>>
{more JSON code}

You donโ€™t have to use the IF() from within AppSheet. You could even have multiple different JSON templates inside one file depending on a certain value from your data.

My personal example (JSON sent to Discord):

Thanks @Bahbus. That will work for my most use cases. But in some cases, I have a virtual columns with JSON snippets which is used by a cloud app via the AppSheet integration.

I also like to use the same snippets within the workflow template.

I could maintain the two set of JSON snippets but it could be an error-prone process.

I also created a feature request that will be from integration side. In cloud-service integrations, provide a way to response a POST request

Top Labels in this Space