I’m sure this is user error in handling a get request and the data that is returned.
What I’m trying to do is to call the calendarific endpoint and then run an if statement on each of the corresponding holiday ‘names’ that comes back. Thought I’d start by just setting the value to a variable to see if I had the correct names (I don’t).
When I ping the URL in a browser, the following comes back:
{"meta":{"code":200},"response":{"holidays":[{"name":"International Day of Charity","description":"The International Day of Charity raises public awareness of what charities do to help minimize poverty.","country":{"id":"us","name":"United States"},"date":{"iso":"2022-09-05","datetime":{"year":2022,"month":9,"day":5}},"type":["United Nations observance"],"urlid":"un\/charity-day","locations":"All","states":"All"},{"name":"Labor Day","description":"Labor Day is a federal holiday in the United States. It gives workers a day of rest and it celebrates their contribution to the American economy.","country":{"id":"us","name":"United States"},"date":{"iso":"2022-09-05","datetime":{"year":2022,"month":9,"day":5}},"type":["National holiday"],"urlid":"us\/labor-day","locations":"All","states":"All"}]}}
How do I properly call the value holiday.name? And how do I handle an if statement comparing against multiple values being returned (multiple holiday ‘names’ being returned per day?).
My goal was to run this once per day and compare the current month & day to get back if today was a national holiday, compare the name to determine other actions to be taken in other rules.
You can take the response and format it in something like jsonformatter.org to make it easier to understand the response structure.
We get the whole response from the HTTP request in $context.response.data, so based on your screenshot it looks like the property you’ve entered is holidays.0.name.
If we look at the formatted response, it’s bit easier to see that the top-level property is response, so the property you would want is response.holidays.0.name.
Or the complete variable would be (quite long):
$context.response.data.response.holidays.0.name
Formatted Response:
{
"meta": {
"code": 200
},
"response": {
"holidays": [
{
"name": "International Day of Charity",
"description": "The International Day of Charity raises public awareness of what charities do to help minimize poverty.",
"country": {
"id": "us",
"name": "United States"
},
"date": {
"iso": "2022-09-05",
"datetime": {
"year": 2022,
"month": 9,
"day": 5
}
},
"type": [
"United Nations observance"
],
"urlid": "un/charity-day",
"locations": "All",
"states": "All"
},
{
"name": "Labor Day",
"description": "Labor Day is a federal holiday in the United States. It gives workers a day of rest and it celebrates their contribution to the American economy.",
"country": {
"id": "us",
"name": "United States"
},
"date": {
"iso": "2022-09-05",
"datetime": {
"year": 2022,
"month": 9,
"day": 5
}
},
"type": [
"National holiday"
],
"urlid": "us/labor-day",
"locations": "All",
"states": "All"
}
]
}
}
Do you have any thoughts on how I should think about constructing an if statement to look for if ‘Labor Day’ exists in one of the ‘name’ values in the array, and then set variables?
Can you clarify what “then set variables” means in this context?
If your goal is basically to get a true/false response if “Labor Day” is in any of the items, I think you could do it with some syntax in the /math lab endpoint.
Take this example which checks to see if an array has a certain exact text match in any of the objects. You can literally enter this into the formula field on https://lab.sharptools.dev and see it in action:
count(filter([{"name": "test"}, {"name": "no match"}], equalText(x.name, "test"))) > 0
We start with an array of objects and apply a filter() to it with a callback that returns true/false.
filter(items, callback)
So our callback is equalText(x.name, "test") which is basically checking of any of the name properties in any of the objects is "test". The result of that is the array of objects which match the condition.
From there, we can check the count of items in the filter() response. If the count of items is greater than 0, then we know at least one item had our match.
So in your case, I believe the example would be a POST to the /math endpoint with a configuration like:
I couldn’t get this to work because the values in the “holidays” weren’t in the format of the [{“name”: “test”}, {“name”: “no match”}] grouping as in the example.
The only way it would work was to put in the exact value in, but I couldn’t get all of the values of the “name” in the format from the example:
Did you try it? Based on the original data you shared, I would’ve expected it to work. It doesn’t have to be an array of objects with only the name. It’s just looking for an array where each object at least has a name property… which your sample data did.
The quotes around the variable would cause the issue. It needs to be the raw variable directly since we’re trying to filter on the array of items rather than a string (which the quotes cause).
Note the lack of quotes around the context variable on the second line. That way it passes the array of holidays as-is…
Just an update that I’ve tested this with our native expressions (that are currently in development) and it works as expected. So we should have you covered once we get the native expressions feature into beta.