Skip to content

Instantly share code, notes, and snippets.

@ozkary
Last active March 22, 2022 20:08
Show Gist options
  • Save ozkary/97159ac6c7d4319d4b05dc5f80af5988 to your computer and use it in GitHub Desktop.
Save ozkary/97159ac6c7d4319d4b05dc5f80af5988 to your computer and use it in GitHub Desktop.
APIM Fluid Scripts . These are policy transformation scripts. Use them to transform XML, JSON documents into JSON documents to match your platform requirements,
/*
@file fluid-for-loop.xml
@description Use this script to convert an input array into json properties. Notice how a command is not added to the last item.
Use hyphen to strip extra blank chrs.
@author ogarcia (ozkary)
@ref https://shopify.dev/api/liquid/objects/for-loops
Input JSON request:
{
"names": [
{"name":"aaa", "value:"111"},
{"name":"bbb", "value:"222"},
{"name":"ccc", "value:"333"}
]
}
Output JSON request:
{
"names": {"aaa":"111", "bbb":"222", "ccc":"333"}
}
*/
<set-body template="liquid">
{%- for item in body.names -%}
{ "names": {
{%- if item.name -%}
"{{item.name}}": "{{item.value}}",
{%- endif -%}
{%- if forloop.last == false -%},{%- endif -%}
}
}
{%- endfor -%}
]
</set-body>
/*
@file fluid-if.xml
@description Use this script to transform a json document using conditional statements. Notice how empty values are filtered.
@author ogarcia (ozkary)
@ref https://shopify.github.io/liquid/basics/operators/
Input JSON request:
{
"items": [
{"name":"Burger", "price:"10"},
{"name":"Frieds", "price:""},
{"name":"Wings", "price:"20"}
]
}
Output JSON request of only valid items
{
"items": {"Burger":"10", "Wings":"20"}
}
*/
<set-body template="liquid">
{%- for item in body.names -%}
{ "names": {
{%- if item.value -%}
"{{item.name}}": "{{item.value}}",
{%- endif -%}
{%- if forloop.last == false -%},{%- endif -%}
}
}
{%- endfor -%}
]
</set-body>
/*
@file fluid-if.xml
@description Use this script to assign variables and do some string operations like replace, remove
@author ogarcia (ozkary)
@ref https://shopify.github.io/liquid/tags/variable/
Input JSON request:
{ author" :
{
"fullname":"Oscar Garcia",
"bio": "Software engineer, **, Product developement ",
}
}
Output JSON request of only valid items
{ author" : {
{
"firstName": "Oscar",
"lastName": "Garcia",
"bio": "Software engineer, *****, Product developement ",
}
}
*/
<set-body template="liquid">
<% assign author = body.author %>
<% fullname = author.fullname | Split:" "%}
<% cleanBio = author.bio | Replace:", *****",""%}
{ "author":
{
"firstName": "{{fullname[0]}}",
"lastName": ""{{fullname[1]}}"",
"bio": "{{cleanBio}}",
}
}
</set-body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment