JSON Logic is a mechanism that can be used to apply logical transformations to JSON values and that is also itself expressed in JSON. It’s a small, safe way to represent one decision.
Togai accepts JSONLogic to provide flexibility of plugging in logic into various parts of the product to handle bespoke requirements elegantly. Togai constructs, which accept JSONLogic are listed below.
In addition to the default operators provided by the JSONLogic, Togai provides certain custom operators to reinforce the power of the platform.
We outline these operators, their input formats, and examples below to help you leverage their functionality effectively.
date
)Convert a date string into a date object.
This operator is only used to make equality check work as expected.
For example, the dates 2024-05-28T00:00:00Z
and 2024-05-27T16:00:00-0800
are contextually equal. But, JsonLogic equality operator ==
does not know that these are dates and hence, treats them as normal strings and returns a falsy response. To overcome this, it is expected to convert the date_string to date (data type) before applying the equality check.
Input Format: [date_string]
Example-1:
Input:
Output: true
Example-2:
Input:
Output: null
If any one of the argument is a Date object, we attempt to cast the other argument into a Date by default.
i.e, All the below JSONLogics computes to true
duration.between
)Using the duration.between
operator computes the time elapsed between two dates and returns duration as string.
Opertion: duration.between
Return value: ISO 8601 Duration as string
Example:
Output: PT744H
Application of JSON Logic (“duration.between”) in Pricing Rules
Consider a scenario where a pricing rule allows 75 free photos per day, but the number of days varies per billing cycle.
This can be implemented by leveraging the start and end dates of the billing cycle which are inputs to pricing rules:
Final Pricing rule computation:
The same logic is constructed using simple jsonLogic builder as follows
date.add
)This operator adds a duration string to a date. Returns null
in case the date or duration string is invalid.
Input Format: [date_string, duration_string]
Example:
Output: 2024-05-29T00:00Z
date.sub
)Subtracts a specified duration from a date.
Input Format: [date_string, duration_string]
Example:
Output: 2024-05-27T00:00Z
duration.of
)The date operations are not natively available in the JSON logic, we did a custom add of the date logic by adding another layer of logic.
This means that for instance the input given as 100 days will be converted into the ISO recommended format of P100D.
INPUT FORMAT: [interval_to_add,_interval_unit]
Supported Interval Unit: MILLIS
, SECONDS
, MINUTES
, HOURS
, DAYS
, WEEKS
, MONTHS
, YEARS
Example:
Output: P100D
This operator allows us to convert a duration to a desired interval.
Input Format: [duration_string, interval_unit]
Supported Interval Unit: MILLIS
, SECONDS
, MINUTES
, HOURS
, DAYS
Example:
Output: 1
JSON Logic is a mechanism that can be used to apply logical transformations to JSON values and that is also itself expressed in JSON. It’s a small, safe way to represent one decision.
Togai accepts JSONLogic to provide flexibility of plugging in logic into various parts of the product to handle bespoke requirements elegantly. Togai constructs, which accept JSONLogic are listed below.
In addition to the default operators provided by the JSONLogic, Togai provides certain custom operators to reinforce the power of the platform.
We outline these operators, their input formats, and examples below to help you leverage their functionality effectively.
date
)Convert a date string into a date object.
This operator is only used to make equality check work as expected.
For example, the dates 2024-05-28T00:00:00Z
and 2024-05-27T16:00:00-0800
are contextually equal. But, JsonLogic equality operator ==
does not know that these are dates and hence, treats them as normal strings and returns a falsy response. To overcome this, it is expected to convert the date_string to date (data type) before applying the equality check.
Input Format: [date_string]
Example-1:
Input:
Output: true
Example-2:
Input:
Output: null
If any one of the argument is a Date object, we attempt to cast the other argument into a Date by default.
i.e, All the below JSONLogics computes to true
duration.between
)Using the duration.between
operator computes the time elapsed between two dates and returns duration as string.
Opertion: duration.between
Return value: ISO 8601 Duration as string
Example:
Output: PT744H
Application of JSON Logic (“duration.between”) in Pricing Rules
Consider a scenario where a pricing rule allows 75 free photos per day, but the number of days varies per billing cycle.
This can be implemented by leveraging the start and end dates of the billing cycle which are inputs to pricing rules:
Final Pricing rule computation:
The same logic is constructed using simple jsonLogic builder as follows
date.add
)This operator adds a duration string to a date. Returns null
in case the date or duration string is invalid.
Input Format: [date_string, duration_string]
Example:
Output: 2024-05-29T00:00Z
date.sub
)Subtracts a specified duration from a date.
Input Format: [date_string, duration_string]
Example:
Output: 2024-05-27T00:00Z
duration.of
)The date operations are not natively available in the JSON logic, we did a custom add of the date logic by adding another layer of logic.
This means that for instance the input given as 100 days will be converted into the ISO recommended format of P100D.
INPUT FORMAT: [interval_to_add,_interval_unit]
Supported Interval Unit: MILLIS
, SECONDS
, MINUTES
, HOURS
, DAYS
, WEEKS
, MONTHS
, YEARS
Example:
Output: P100D
This operator allows us to convert a duration to a desired interval.
Input Format: [duration_string, interval_unit]
Supported Interval Unit: MILLIS
, SECONDS
, MINUTES
, HOURS
, DAYS
Example:
Output: 1