Creating input from JSON Schema

In this article we'll quickly set up a ruleset by importing the input and entities from a JSON Schema file.

Let's first create a subscriber ruleset that calculates the subscriber's age and the subscription amount.

  1. Create a ruleset named Subscriber.

  2. Create a constant named SubscriptionCostPerDay and set its value to 0.10.

Now the ruleset needs input; we're going to take a shortcut and define the input through importing a JSON schema. This will result in automatic creation of Input and Entities. Entities are like objects, and you can create them by hand, but if you have a JSON schema, there is a much quicker and easier way:

  1. Download the SubscriberSchema.json file to your workstation.

  1. Open your file manager and head over to the folder where the SubscriberSchema.json file is located.

  2. Drag the SubscriberSchema.json file to the Entities section in Rulecube and drop it. ↳ Rulecube automatically creates a SubscriberObject entity based on the JSON schema:

    1. Alternatively, if your browser does not allow drag and drop, you may upload this file under the Ruleset's Settings > Tools page.

This is cool. Just drop a JSON schema, and you've got a basic entity. You can do the same with an XSD, by the way. For now, you need to tweak the entity a little:

  1. Click on the SubscriberObject entity and add a calculated property named Age and set its Expression to:

AGE(dateOfBirth)
  1. Add another calculated property named SubscriptionAmount and set its Expression to calculate the subscription amount based on the remaining days in the current year and the subscription cost per day:

DAYDIFF(TODAY(), TODAY().endOfYear()) * SubscriptionCostPerDay

That's it; your entity is ready and includes the calculated properties that you need in your software. To finish the ruleset, you have to set its input and output based on the entity:

  1. Create an input named Subscriber and set its type to SubscriberObject.

  2. Select the Output checkbox.

  3. Save the ruleset and try it out.

Done. You're ready now to call your ruleset via its API from any software you like. Click Docs > Examples for a fetch snippet to call the ruleset from JavaScript.

What's next?

Follow our How-to Guides to add more functionality.