Format Json Response Problems

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.

Thanks.

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"
      }
    ]
  }
}

Perfect, thanks for this – super helpful!

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?

Thanks so much.

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:

{
"formula": "count(filter(holidays, equalText(x.name, 'Labor Day'))) > 0",
"holidays": $context.response.data.response.holidays
}

That would return a true or false in the $context.response.data.result variable.

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:

Think I need to try and come up with a different approach.

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.

Yes, I tried that specific one (shown below) as well as many others with no luck.

And here is the log detail. The actual text message I received said: “invalid variable”

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…

I did try that at first, but I get the ‘invalid JSON’ error and I can’t update the action.

image

Dang! That looks like a bug as it is possibly a valid JSON string…

It’s Schrödinger’s JSON string… might be valid, might not… have to open the box to find out. :person_facepalming:

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. :slight_smile:

1 Like

@josh doesn’t look like this got pushed with the latest set of items in the release, just making sure however.

Yes, it works with the native expressions. You can reference the variables directly, so it would look something like:

Since expressions are still in beta, we can continue the discussion over in the beta thread. :slight_smile: