Hi there, I read about the default / alternation syntax for string expansion, like %{%{Foo}:-bar}. But what I needed is this: if (foo != '' ) ? '[ %{foo} ]' : 'null'. This is for a JSON request string in a REST module configuration. The request property in question must be a JSON array ( [ .. ] ) or 'null'. I could use Unlang IF and declare the complete JSON request in different versions depending on foo. But I would prefer to do this with expansion inside the JSON string definition, if possible. Cheers Till
On Apr 13, 2026, at 4:59 PM, g4-lisz@tonarchiv.ch wrote:
I read about the default / alternation syntax for string expansion, like %{%{Foo}:-bar}.
But what I needed is this: if (foo != '' ) ? '[ %{foo} ]' : 'null'.
This is for a JSON request string in a REST module configuration. The request property in question must be a JSON array ( [ .. ] ) or 'null'.
I could use Unlang IF and declare the complete JSON request in different versions depending on foo. But I would prefer to do this with expansion inside the JSON string definition, if possible.
You'll have to use 'if'. There is a mods-available/json, and you can do: %{json_encode:&foo} Except that if "foo" doesn't exist, it returns an empty json object: "{}" Alan DeKok
Alan DeKok via Freeradius-Users <freeradius-users@lists.freeradius.org> wrote:
On Apr 13, 2026, at 4:59 PM, g4-lisz@tonarchiv.ch wrote:
I read about the default / alternation syntax for string expansion, like %{%{Foo}:-bar}.
But what I needed is this: if (foo != '' ) ? '[ %{foo} ]' : 'null'.
This is for a JSON request string in a REST module configuration. The request property in question must be a JSON array ( [ .. ] ) or 'null'.
I could use Unlang IF and declare the complete JSON request in different versions depending on foo. But I would prefer to do this with expansion inside the JSON string definition, if possible.
You'll have to use 'if'. There is a mods-available/json, and you can do:
%{json_encode:&foo}
Except that if "foo" doesn't exist, it returns an empty json object: "{}"
You could use another attribute and assign the json text to that attribute using if/else, then base the REST stuff off that attribute. That at least keeps your REST query DRY. There are some temporary internal-use-only attributes in the dictionary for stuff like this. If you are running a version that has mod_json you should use it. Performing data sanitation by hand is cringy.
April 14, 2026 at 4:12 PM, "Brian Julin" <BJulin@clarku.edu mailto:BJulin@clarku.edu?to=%22Brian%20Julin%22%20%3CBJulin%40clarku.edu%3E > wrote:
Alan DeKok via Freeradius-Users <freeradius-users@lists.freeradius.org> wrote:
On Apr 13, 2026, at 4:59 PM, g4-lisz@tonarchiv.ch wrote:
I read about the default / alternation syntax for string expansion, like %{%{Foo}:-bar}.
But what I needed is this: if (foo != '' ) ? '[ %{foo} ]' : 'null'.
This is for a JSON request string in a REST module configuration. The request property in question must be a JSON array ( [ .. ] ) or 'null'.
I could use Unlang IF and declare the complete JSON request in different versions depending on foo. But I would prefer to do this with expansion inside the JSON string definition, if possible.
You'll have to use 'if'. There is a mods-available/json, and you can do:
%{json_encode:&foo}
Except that if "foo" doesn't exist, it returns an empty json object: "{}"
You could use another attribute and assign the json text to that attribute using if/else, then base the REST stuff off that attribute. That at least keeps your REST query DRY. There are some temporary internal-use-only attributes in the dictionary for stuff like this.
Hi Brian, Thanks for the hint! That's actually a simple solution I hadn't thought of. Meanwhile, I wrote my own xlat function. But for some reason, my last email to the list didn't get through. Cheers, Till
April 14, 2026 at 2:27 PM, "Alan DeKok via Freeradius-Users" <freeradius-users@lists.freeradius.org mailto:freeradius-users@lists.freeradius.org?to=%22Alan%20DeKok%20via%20Freeradius-Users%22%20%3Cfreeradius-users%40lists.freeradius.org%3E > wrote:
On Apr 13, 2026, at 4:59 PM, g4-lisz@tonarchiv.ch wrote:
I read about the default / alternation syntax for string expansion, like %{%{Foo}:-bar}.
But what I needed is this: if (foo != '' ) ? '[ %{foo} ]' : 'null'.
This is for a JSON request string in a REST module configuration. The request property in question must be a JSON array ( [ .. ] ) or 'null'.
I could use Unlang IF and declare the complete JSON request in different versions depending on foo. But I would prefer to do this with expansion inside the JSON string definition, if possible.
You'll have to use 'if'. There is a mods-available/json, and you can do:
%{json_encode:&foo}
Except that if "foo" doesn't exist, it returns an empty json object: "{}"
Thanks for your reply, Alan. I've decided that it's best to add our own xlat function. We use a patched Rest module anyway: if (strcmp(fmt, "serviceIds") == 0) { if ((vp = fr_pair_find_by_num(request->config, PW_CUST_SERVICE_ID, 0, T_OP_EQ)) != NULL) { len = snprintf(out, freespace, "[ %s ]", vp->data.strvalue); } else { len = snprintf(out, freespace, "null"); } out += len; freespace -= len; } *out = '\0'; Now we can simply use: authenticate { .... data: {\ .... "serviceIds": %{vendor:serviceIds},\ .... } Cheers, Till
participants (3)
-
Alan DeKok -
Brian Julin -
g4-lisz@tonarchiv.ch