{"id":24944528,"url":"https://github.com/celadari/json-logic-scala","last_synced_at":"2026-03-10T10:06:34.955Z","repository":{"id":45966627,"uuid":"180032878","full_name":"celadari/json-logic-scala","owner":"celadari","description":"Build complex rules, serialize them as JSON, and execute them in Scala","archived":false,"fork":false,"pushed_at":"2021-11-24T01:16:38.000Z","size":3042,"stargazers_count":5,"open_issues_count":1,"forks_count":5,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-10T04:50:36.898Z","etag":null,"topics":["expressions","json","json-logic","scala"],"latest_commit_sha":null,"homepage":"https://jsonlogicscala.com","language":"Scala","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/celadari.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-04-07T22:46:26.000Z","updated_at":"2023-01-21T12:59:10.000Z","dependencies_parsed_at":"2022-08-24T07:20:35.242Z","dependency_job_id":null,"html_url":"https://github.com/celadari/json-logic-scala","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/celadari/json-logic-scala","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/celadari%2Fjson-logic-scala","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/celadari%2Fjson-logic-scala/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/celadari%2Fjson-logic-scala/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/celadari%2Fjson-logic-scala/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/celadari","download_url":"https://codeload.github.com/celadari/json-logic-scala/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/celadari%2Fjson-logic-scala/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30329698,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-10T05:25:20.737Z","status":"ssl_error","status_checked_at":"2026-03-10T05:25:17.430Z","response_time":106,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["expressions","json","json-logic","scala"],"created_at":"2025-02-02T19:54:22.820Z","updated_at":"2026-03-10T10:06:34.934Z","avatar_url":"https://github.com/celadari.png","language":"Scala","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Json Logic Scala\n\n[![Scala versions](https://img.shields.io/badge/scala-2.11%7C2.12%7C2.13-blue.svg)]()\n[![Linter](https://github.com/celadari/json-logic-scala/actions/workflows/linter.yml/badge.svg)](https://github.com/celadari/json-logic-scala/actions/workflows/linter.yml)\n[![Unittests](https://github.com/celadari/json-logic-scala/actions/workflows/tests.yml/badge.svg)](https://github.com/celadari/json-logic-scala/actions/workflows/tests.yml)\n[![Build](https://github.com/celadari/json-logic-scala/actions/workflows/build.yml/badge.svg)](https://github.com/celadari/json-logic-scala/actions/workflows/build.yml)\n[![codecov](https://codecov.io/gh/celadari/json-logic-scala/branch/v2/graph/badge.svg?token=9LEB1I50R3)](https://codecov.io/gh/celadari/json-logic-scala)\n[![License](https://img.shields.io/badge/license-MIT-blue.svg)]()\n\n![Json Logic Scala](.img/jsonlogicscala_logo.jpg)\n\n---\n\n\nBuild complex rules, serialize them as JSON, and execute them in Scala.\n\nJson-logic-scala enables you to serialize in JSON format logical expressions.\nIt also enables you to load a scala object from a logical expression/JSON.\n\nDue to Scala's strong static typed language nature, json-logic-scala requires JSON to add tell type in json.\n\n### Why would you use json-logic-scala ?\nThe [JsonLogic format](http://jsonlogic.com/) is designed to allow you to share rules (logic) between\nfront-end and back-end code (regardless of language difference), even to store\nlogic along with a record in a database.\n\nLogic that has been exported from another language can be applied quickly on\nscala.\n\n### Scala Versions\n\nThis project is compiled, tested, and published for the following Scala versions:\n* 2.11.12\n* 2.12.6\n* 2.13.2\n\n# Table of Contents\n1. [Installation]()\n\n2. [Main concepts: Boolean-Algebra-Tree]()\n\n    2.1 [`ComposeLogic`: Internal Node]()\n\n    2.2 [`ValueLogic`: Leaf Node]()\n\n3. [Example]()\n\n4. [Read/Write json]()\n\n    4.1 [Type information]()\n\n    4.2 [Default built-in types]()\n\n    4.3 [Read json: Define `Deserializer`]()\n\n    4.4 [Write json: Define `Serializer`]()\n\n5. [Evaluating logical expression: `eval` method]()\n6. [Define custom operators]()\n7. [Define EvaluatorValueLogic]()\n8. [Define objects as Java services]()\n9. [Scaladoc API]()\n10. [Contributors]()\n\n\n## 1. Installation\n\nTo get started, add json-logic-scala as a dependency to your project:\n\n* sbt\n    ```sbt\n    libraryDependencies += \"com.celadari\" %% \"json-logic-scala\" % \"2.0\"\n    ```\n\n* Gradle\n    ```gradle\n    compile group: 'com.celadari', name: 'json-logic-scala_2.12', version: '2.0'\n    ```\n* Maven\n    ```maven\n    \u003cdependency\u003e\n      \u003cgroupId\u003ecom.celadari\u003c/groupId\u003e\n      \u003cartifactId\u003eplay-json_2.12\u003c/artifactId\u003e\n      \u003cversion\u003e2.0\u003c/version\u003e\n    \u003c/dependency\u003e\n    ```\nJson-logic-scala supports Scala 2.11 and 2.12. Choosing the right JAR is automatically managed in sbt. If you're using Gradle or Maven then you need to use the correct version in the artifactId.\n\n## 2. Main concepts: Boolean-Algebra-Tree\nBoolean expressions are complex boolean statements composed of atoms, unary, binary and multiple operators.\nAtoms are assigned a value, and can be fed to a binary or unary expression.\nFor example, the logical expression\n\n\u003cp align=\"center\"\u003e\n    \u003cimg src=\".img/logical_expression.png\" alt=\"drawing\" width=\"300\"/\u003e\n\u003c/p\u003e\n\ncan be parsed to the following Abstract Syntax Tree:\n\n\u003cp align=\"center\"\u003e\n    \u003cimg src=\".img/boolean_logical_tree.png\" alt=\"drawing\" width=\"500\"/\u003e\n\u003c/p\u003e\n\nA tree representation of the logical expression is very convenient. After isolating the outermost operator of the\nexpression (the operator which is enclosed with the fewest amount of parentheses), the logical expression can be split on\nsaid operator into different branches representing themselves logical expressions. These different expressions can be further\nsplit into different branches until reaching leaves Node which represent single atoms.\n\nEvaluating the logical expression in\nits tree representation is evaluated recursively. Each Internal Node needs to have its children nodes evaluated before\nbeing evaluated. Leaf Nodes represent variables/values.\n\nA boolean decision tree is represented by the `JsonLogicCore` class - which has two subtypes:\n\n### 2.1 `ComposeLogic`: Internal Node.\nA `ComposeLogic` class is an Internal Node in the boolean-algebra-tree.\nIt is defined by two fields:\n- `operator`: `String` the codename of the operator.\n- `conditions`: `Array[JsonLogicCore]` array of sub-conditions this node applies to.\n\n### 2.2 `ValueLogic`: Leaf Node.\nIt represents a basic value for an operand in order to produce a condition.\nIt is defined by two fields.\n- `operator`: `String` whose value is supposed to be always `\"var\"`.\n- `valueOpt`: `Option[T]` the value object itself to feed an operand.\n- `typeOpt`: `Option[TypeValue]` the type associated to this value.\n- `variableNameOpt`: `Option[String]` name of variable it references if inside composition function.\n- `pathNameOpt`: `Option[String]` key associated to this value in json snippet.\n\n## 3. Example\nLet's suppose you have a parquet/csv file on disk and you want to remember/transfer\nfiltering rules before loading it.\n\n| price (€) | quantity | label    | label2    | clientID | date                |\n|-----------|----------|----------|----------|----------|---------------------|\n| 54        | 2        | t-shirts | t-shirts | 245698   | 2018-01-12 09:12:00 |\n| 68        | 1        | pants    | shoes    | 478965   | 2019-07-24 15:24:00 |\n| 10        | 2        | sockets  | hat      | 478963   | 2020-02-14 16:22:00 |\n|...........|..........|..........|..........|..........|.....................|\n\nLet's suppose we are only interested in rows which satisfy logical expression:\n\u003cp align=\"center\"\u003e\n    \u003cimg src=\".img/logical_expression.png\" alt=\"drawing\" width=\"200\"/\u003e\n\u003c/p\u003e\nIf you want to store the logic (logical expression) in an universal format that can\nbe shared between scala, R, python code you can store in jsonLogic format.\n\nFor the logic:\n```json\n{\n  \"and\": [{\n            \"\u003c=\": [\n                    {\"var\": \"colA\", \"type\": \"column\"},\n                    {\"var\": \"valA\", \"type\": \"value\"}\n                  ]\n          },\n          {\n            \"!=\": [\n                    {\"var\": \"colB\", \"type\": \"column\"},\n                    {\"var\": \"colC\", \"type\": \"column\"}\n                  ]\n          }\n         ]\n}\n```\nFor the values:\n```json\n{\n    \"colA\": {\"name\": \"price (€)\"},\n    \"valA\": {\"value\": 20, \"type\": \"int\"},\n    \"colB\": {\"name\": \"label\"},\n    \"colC\": {\"name\": \"label2\"}\n}\n```\n\n## 4. Read/Write json\n\nTo use **Json Logic Scala**, you should start by defining or importing a\n`JsonLogicCore` instance (we'll see how to evaluate it latter below).\n\n### 4.1 Type information\nType is annotated after the field `\"type\"` in a `\"var\"` operator JSON\n(i.e. the leaf node in corresponding syntax tree).\n\n#### Simple Type\n\nA simple type is simply defined by its `codename` field value.\n\n```json\n[{\n    \"...\": [\n            {\"var\":  \"price_value\", \"type\":  {\"codename\":  \"int\"}}\n    ]\n},\n{\n    \"price_value\": ...\n}]\n```\n\n#### Higher Type (option|array|map)\n\nA higher type is a composition of simple and/or higher types.\nA higher type represents generic types in Scala like arrays, options, and maps.\n\nIt is recursively defined by its `codename` field value and its `paramType` field value.\n\n**\n\nIn the following example, the variable `price_values` is to be parsed as an `Array[Int]`\n\n```json\n[{\n    \"...\": [\n            {\"var\":  \"price_values\", \"type\":  {\"codename\":  \"array\", \"paramType\": {\"codename\":  \"int\"}}}\n    ]\n},\n{\n    \"price_values\": ...\n}]\n```\n\n### 4.2 Default built-in types\n*Json-logic-scala* **comes with built-in naming convention for basic types**\n\n| `\"type\"` field | Scala type |\n|:--------------:|:----------:|\n|`\"byte\"`        | `Byte`     |\n|`\"short\"`       | `Short`    |\n|`\"int\"`         | `Int`      |\n|`\"long\"`        | `Long`     |\n|`\"string\"`      | `String`   |\n|`\"float\"`       | `Float`    |\n|`\"double\"`      | `Double`   |\n|`\"boolean\"`     | `Boolean`  |\n|`\"array\"`       | `Array`    |\n|`\"map\"`         | `Map`      |\n|`\"option\"`      | `Option`   |\n\n\n\n### 4.3 Read json: Define Deserializer\nThe `Deserializer` utility class converts JsonLogic-Typed data\ninto Scala data structure.\n\nConfigure the `Deserializer` class with the `DeserializerConf` object.\nThis object defines how types in JsonLogic-Typed map to a Scala data structure.\nIf no custom object is provided, the default `DeserializerConf` is used .\n\n```scala\nimport play.api.libs.json.Json\n\nval jsonString: String = ...\nimplicit val deserializerConf = DeserializerConf.createConf(...)\nimplicit val deserializer = new Deserializer()\nval jsonLogicCore = deserializer.deserialize(jsonString)\n```\n\n### 4.4 Write json: Define Serializer\nThe `Serializer` utility class converts a Scala data structure to JsonLogic-Typed\ndata.\n\nConfigure the`Serializer` class with the `SerializerConf` object.\nThis object defines how Scala data structures map to the JsonLogic-Typed format.\nIf no custom object is provided, the default `SerializerConf` is used .\n\n```scala\nimport play.api.libs.json.Json\n\nval jsonLogicCore: JsonLogicCore = ...\nimplicit val serializerConf = SerializerConf.createConf(...)\nimplicit val serializer = new Serializer()\nval jsonString = serializer.serialize(jsonLogicCore)\n```\n\n\n## 5. Evaluating logical expression: `eval` method\nEvaluating a logical expression and getting its result is the main goal in most cases.\nGenerally, logic/rules are received from another language/application and we want to apply this logic\nto our Scala program.\nEvaluating the logical expression is performed by applying a\n[reduce](https://en.wikipedia.org/wiki/Fold_(higher-order_function)) function to the boolean-algebra-tree.\n\n\n## 6. Define custom operators\n\nFor more information please check [online documentation](https://jsonlogicscala.com).\n\n## 7. Define EvaluatorValueLogic\n\nFor more information please check [online documentation](https://jsonlogicscala.com).\n\n\n## 8. Define objects as Java services\n\nFor more information please check [online documentation](https://jsonlogicscala.com).\n\n## 9. Scaladoc API\n\nAPI Documentation available [here](https://www.jsonlogicscala.com/_api/api.html).\n\n## 10. Contributors\n* Charles-Edouard LADARI\n* Matt DODSON\n\n## License\n\n*Json Logic Scala* is licensed under the MIT License.\n\n    MIT License\n\n    Copyright (c) 2019 celadari\n\n    Permission is hereby granted, free of charge, to any person obtaining a copy\n    of this software and associated documentation files (the \"Software\"), to deal\n    in the Software without restriction, including without limitation the rights\n    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n    copies of the Software, and to permit persons to whom the Software is\n    furnished to do so, subject to the following conditions:\n\n    The above copyright notice and this permission notice shall be included in all\n    copies or substantial portions of the Software.\n\n    THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n    SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fceladari%2Fjson-logic-scala","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fceladari%2Fjson-logic-scala","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fceladari%2Fjson-logic-scala/lists"}