← Back to blog

The Potential of ChatGPT + JsonLogic

The Potential of ChatGPT + JsonLogic

Introduction

I had been thinking about flexible system customization using JsonLogic as a topic for a tech blog post, but I wanted to add an extra twist. I wondered what would happen if I combined it with the recently popular ChatGPT, and the results were surprisingly good — so I rewrote the post with a new title.

About JsonLogic

JsonLogic is a general-purpose rule engine that we currently use at NearMe for complex fare calculations and similar tasks. For example, the fares presented to users may be adjusted based on the time of day or how far in advance a ride is booked, in order to balance supply and demand. We also have fare tables negotiated with taxi companies to calculate dispatch costs, which involve a complex interplay of variables such as area, number of passengers (adults and children), amount of luggage, and time. One approach to handling these calculations is to hardcode them, but that makes updates difficult and doesn’t scale well. Another option is to store configuration parameters in a database, but that can result in too many parameters and make it hard to keep the logic extensible. JsonLogic solves these problems elegantly.

Let’s ask ChatGPT about JsonLogic.

  • What is JsonLogic?
JsonLogic is a rule engine expressed in JSON format that provides a simple grammar for handling data. With JsonLogic, you can easily perform tasks such as simple conditional branching, arithmetic calculations, and data filtering.

JsonLogic defines rules using simple structures similar to JavaScript objects. Rules support basic conditional branching like if/else statements, arithmetic operations, string manipulation, array operations, and combinations of nested rules.

JsonLogic is commonly used in areas such as business rules and automation of data processing. JsonLogic's simple syntax and flexibility allow data processing and business logic automation to be carried out efficiently and scalably.

A nicely concise explanation. As a supplement: JsonLogic does not allow variable assignment, loops, function definitions, or GOTO statements within rules, so it is side-effect-free and guaranteed to terminate. However, you can explicitly register custom functions and call them, so you can generally accomplish what you need.

Let me show some practical examples.

> jsonLogic.apply({ "+" : [1, 1] })
2

Here, the argument to apply represents the rule 1 + 1, and calling apply evaluates it and outputs 2.

Rules can also be nested:

> jsonLogic.apply({ "*": [{ "+" : [1, 1] }, 3] })
6

This represents (1 + 1) * 3.

Variables are defined as follows:

> jsonLogic.apply({ "+" : [{ "var" : "x" }, { "var" : "y" }] }, { x: 1, y: 2 })
3

Here, the rule represents x + y, and with x = 1 and y = 2, it outputs 3.

Supported operators include not only the four basic arithmetic operations, but also if statements, comparison operators, logical operators, and array operations (reference).

You can also use custom-defined functions like this (reference):

> jsonLogic.add_operation("sqrt", Math.sqrt);
> jsonLogic.apply({ "sqrt":2 })
1.4142135623730951

Generating JsonLogic with ChatGPT

Let’s see whether ChatGPT can handle JsonLogic.

The version used at the time of testing was “ChatGPT Mar 23 Version” (without ChatGPT Plus).

  • Please express 1+1 in JsonLogic.
Here is an example of 1+1 expressed in JsonLogic:

{
  "+" : [1, 1]
}

This expression means adding two numbers 1 using the + operator. JsonLogic is similar to JavaScript syntax, but because you don't need to use parentheses like in JavaScript, expressions are concise and intuitive.

Impressive! It correctly returned a JsonLogic expression. It’s surprising that it understands a relatively niche syntax like JsonLogic.

Let’s try a more realistic problem.

  • Please express the following in JsonLogic: output a standard fare of ¥3,000 or a late-night fare of ¥4,000 depending on the time. The late-night fare applies before 5 AM or from 10 PM onward. Use the variable hour for the time.
Here is an example of logic that outputs a standard fare or late-night fare based on the time using JsonLogic:

{
  "if": [
    { ">=": [ { "var": "hour" }, 22 ] },
    4000,
    {
      "if": [
        { "<": [ { "var": "hour" }, 5 ] },
        4000,
        3000
      ]
    }
  ]
}

In this logic, if the current hour is 22 or later, the late-night fare (¥4,000) applies. Otherwise, if the hour is less than 5, the late-night fare applies; otherwise the standard fare (¥3,000) applies.

Impressive. It understood both the "if" statement and the "var" variable. It also correctly interpreted “before 5 AM” as strictly less than 5 rather than less than or equal to 5.

That said, it did take some trial and error to get this result. It would occasionally make syntax errors, and the likelihood increased when the prompt was imprecise. Also, depending on how the prompt was worded, it sometimes interpreted the late-night surcharge as an additive amount and returned ¥3,000 + ¥4,000.

Let’s refine the output a bit further.

  • Please eliminate the nested "if" using the "or" operator.
Here is an example of eliminating the nested "if" using the "or" operator:

{
  "if": [
    {
      "or": [
        { ">=": [ { "var": "hour" }, 22 ] },
        { "<": [ { "var": "hour" }, 5 ] }
      ]
    },
    4000,
    3000
  ]
}

In this logic, the "or" operator determines whether the current hour is 22 or later, or less than 5. If either condition is true, the late-night fare (¥4,000) applies; otherwise the standard fare (¥3,000) applies.

The desired result was obtained. From the relationship of the nested "if", it was able to reduce the logic to an "or" condition in a kind of inferential step.

Let’s also ask it to generate test code.

  • Please generate test code in JavaScript along with test cases. Factor out the JsonLogic expression so it is shared across all test cases.
Here is an example that shares the JsonLogic expression and generates test code with test cases in JavaScript:

const JsonLogic = require('json-logic-js');

// Shared JsonLogic expression
const pricingRule = {
  "if": [
    {
      "or": [
        { ">=": [ { "var": "hour" }, 22 ] },
        { "<": [ { "var": "hour" }, 5 ] }
      ]
    },
    4000,
    3000
  ]
};

// Function to generate a test case
function generateTestCase(hour, expectedPrice) {
  return {
    input: { hour },
    expectedOutput: expectedPrice
  };
}

// Function to generate test code
function generateTestCode(pricingRule) {
  const testCases = [
    generateTestCase(0, 4000),
    generateTestCase(3, 4000),
    generateTestCase(5, 3000),
    generateTestCase(10, 3000),
    generateTestCase(20, 3000),
    generateTestCase(22, 4000),
    generateTestCase(23, 4000),
  ];

  testCases.forEach(({ input, expectedOutput }) => {
    const actualOutput = JsonLogic.apply(pricingRule, input);
    console.log(`Input: ${JSON.stringify(input)}, Expected Output: ${expectedOutput}, Actual Output: ${actualOutput}`);
    console.assert(actualOutput === expectedOutput, `Test Failed: ${JSON.stringify(input)}`);
  });
}

// Run the test code
generateTestCode(pricingRule);

The test code returned was better than expected. Boundary values are also taken into account.

Translating JsonLogic with ChatGPT

This time, let’s do the reverse: convert a JsonLogic expression back into plain language.

  • Please express the following JsonLogic expression in natural language: {"if":[{"or":[{">=":[{"var":"hour"},22]},{"<":[{"var":"hour"},5]}]},4000,3000]} Note that the output represents a fare.
This JsonLogic expression can be expressed in natural language as follows:

"If the current hour is 22 or later, or less than 5, set the fare to ¥4,000; otherwise, set the fare to ¥3,000."

Here, "hour" referenced by "var" is assumed to be a variable representing the time at which the expression is evaluated. "4000" and "3000" represent the fares applied in each case.

Correctly translated.

Let’s try a more complex example.

  • Please express the following JsonLogic expression in natural language: {"if":[{"and":[{"in":[{"var":"from_area"},["area1","area2","area3"]]},{"in":[{"var":"to_area"},["itami"]]}]},3500,{"and":[{"in":[{"var":"from_area"},["area1","area2","area3"]]},{"in":[{"var":"to_area"},["kanku"]]}]},4500,{"and":[{"in":[{"var":"from_area"},["area4","area5","area6"]]},{"in":[{"var":"to_area"},["itami"]]}]},4000,{"and":[{"in":[{"var":"from_area"},["area4","area5","area6"]]},{"in":[{"var":"to_area"},["kanku"]]}]},5000,3000]} Note that the output represents a fare.
This JsonLogic expression can be expressed in natural language as follows:

"If the origin is area1, area2, or area3 and the destination is Itami, set the fare to ¥3,500. If the origin is area1, area2, or area3 and the destination is Kansai (Kanku), set the fare to ¥4,500. If the origin is area4, area5, or area6 and the destination is Itami, set the fare to ¥4,000. If the origin is area4, area5, or area6 and the destination is Kansai (Kanku), set the fare to ¥5,000. If none of the above conditions apply, set the fare to ¥3,000."

Here, "from_area" and "to_area" referenced by "var" are assumed to be variables representing the origin and destination, respectively. The fare varies depending on the condition.

Much more readable. The translation is also correct.

Conclusion

We have demonstrated that ChatGPT correctly understands and handles JsonLogic. At present, vague or imprecise descriptions can lead to unintended results, but it is already at a level that is practical to use. Further improvements in accuracy can also be expected in the future.

JsonLogic brings a high degree of flexibility to systems while remaining relatively safe and easy to handle on the system side. One downside was that you had to learn the syntax, which made it somewhat difficult to read and write — but using ChatGPT for translation should resolve that. Looking ahead, it seems plausible that ChatGPT could control a system via JsonLogic, for example by letting users issue voice commands to ChatGPT to execute tasks that trigger certain actions under specific conditions.

Finally, NearMe is hiring engineers! If you are interested, please check out the link below.

Author: Kenji Hosoda

Author: Kenji Hosoda