{"id":17248758,"url":"https://github.com/nomisrev/kotlinx-serialization-jsonpath","last_synced_at":"2025-04-13T02:13:18.218Z","repository":{"id":40295770,"uuid":"486725431","full_name":"nomisRev/kotlinx-serialization-jsonpath","owner":"nomisRev","description":"KotlinX Serialization JsonElement DSL based on Arrow Optics","archived":false,"fork":false,"pushed_at":"2025-04-08T02:33:49.000Z","size":941,"stargazers_count":39,"open_issues_count":9,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-13T02:13:10.943Z","etag":null,"topics":["arrow-kt","hacktoberfest","json","jsonpath","kotlin","kotlindsl","kotlinx-serialization","optics"],"latest_commit_sha":null,"homepage":"https://nomisrev.github.io/kotlinx-serialization-jsonpath/","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nomisRev.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-04-28T19:35:32.000Z","updated_at":"2025-04-07T15:15:30.000Z","dependencies_parsed_at":"2023-09-25T05:09:18.397Z","dependency_job_id":"5ba5b287-0ca2-402f-940d-06abc24fb715","html_url":"https://github.com/nomisRev/kotlinx-serialization-jsonpath","commit_stats":{"total_commits":136,"total_committers":4,"mean_commits":34.0,"dds":0.3602941176470589,"last_synced_commit":"bdbade74463ddf18897f5f14e51bf06de6a0b123"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":"arrow-kt/Arrow-KMP-Template","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nomisRev%2Fkotlinx-serialization-jsonpath","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nomisRev%2Fkotlinx-serialization-jsonpath/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nomisRev%2Fkotlinx-serialization-jsonpath/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nomisRev%2Fkotlinx-serialization-jsonpath/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nomisRev","download_url":"https://codeload.github.com/nomisRev/kotlinx-serialization-jsonpath/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248654094,"owners_count":21140236,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["arrow-kt","hacktoberfest","json","jsonpath","kotlin","kotlindsl","kotlinx-serialization","optics"],"created_at":"2024-10-15T06:42:04.506Z","updated_at":"2025-04-13T02:13:18.199Z","avatar_url":"https://github.com/nomisRev.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Module KotlinX Serialization JsonPath\n\n[![Maven Central](https://img.shields.io/maven-central/v/io.github.nomisrev/kotlinx-serialization-jsonpath?color=4caf50\u0026label=latest%20release)](https://maven-badges.herokuapp.com/maven-central/io.github.nomisrev/kotlinx-serialization-jsonpath)\n[![Latest snapshot](https://img.shields.io/badge/dynamic/xml?color=orange\u0026label=latest%20snapshot\u0026prefix=v\u0026query=%2F%2Fmetadata%2Fversioning%2Flatest\u0026url=https%3A%2F%2Fs01.oss.sonatype.org%2Fservice%2Flocal%2Frepositories%2Fsnapshots%2Fcontent%2Fio%2Fgithub%2Fnomisrev%2Fkotlinx-serialization-jsonpath%2Fmaven-metadata.xml)](https://s01.oss.sonatype.org/service/local/repositories/snapshots/content/io/github/nomisrev)\n\nJsonPath offers a simple DSL to work with JsonElement from Kotlinx Serialization Json,\nthis allows you to easily work with JSON in Kotlin in a typed manner.\nSimply add the following dependency as `implementation` in the `build.gradle` dependencies` block.\n\n```groovy\ndependencies {\n  implementation(\"io.github.nomisrev:kotlinx-serialization-jsonpath:0.2.0\")\n}\n```\n\nLet's dive right in with following `JSON_STRING` as input `JsonElement` that models a simple company.\n\n\u003c!--- TEST_NAME ReadMeSpec --\u003e \n\n```json\n{\n  \"name\": \"Arrow\",\n  \"address\": {\n    \"city\": \"Functional Town\",\n    \"street\": {\n      \"number\": 1337,\n      \"name\": \"Functional street\"\n    }\n  },\n  \"employees\": [\n    {\n      \"name\": \"John\",\n      \"lastName\": \"doe\"\n    },\n    {\n      \"name\": \"Jane\",\n      \"lastName\": \"doe\"\n    }\n  ]\n}\n```\n\nGiven this `JsonElement` we can _select_ the `name` from the `JsonElement`.\nThis gives us an `Optional` from the _root_ `JsonElement` to the `name` property with type `String`. \nWe can then use this _JsonPath_ to _modify_ the original `JsonElement`'s `name` property,\nthis gives us back _a new_ `JsonElement` with the _name_ modified according to the passed `String::uppercase` function.\n\n\u003c!--- INCLUDE\nfun main(): Unit {\n----- SUFFIX\n}\n--\u003e\n```kotlin\n  val json: JsonElement = Json.decodeFromString(JSON_STRING)\n  val name: Optional\u003cJsonElement, String\u003e = JsonPath.select(\"name\").string\n  println(name.modify(json, String::uppercase))\n```\n\u003e You can get the full code [here](src/jvmTest/kotlin/example/example-readme-01.kt).\n\n```text\n{\"name\":\"ARROW\",\"address\":{\"city\":\"Functional Town\",\"street\":{\"number\":1337,\"name\":\"Functional street\"}},\"employees\":[{\"name\":\"John\",\"lastName\":\"doe\"},{\"name\":\"Jane\",\"lastName\":\"doe\"}]}\n```\n\nAs we've seen above we can _select_ a property from a _JsonObject_,\nbut what if we want to access deeply nested properties in our `JsonElement`?\nFor that we can use _path_ which allows you to _select_ deeply nested properties using the _dot (.) notation_ you might know from Javascript.\n\nBelow we _select_ the _address_ `JsonObject`,\nand from the _address_ `JsonObject` we then _select_ the _street_ `JsonObject`,\nto then finally _select_ the _name_ of the _street_.\n\nThis again returns us an `Optional\u003cJsonElement, String\u003e`, which we use to _modify_ the `address.street.name`.\nWe then also _extract_ the value using `getOrNull` which returns us the desired _path_ in our `JsonElement`,\nor it returns _null_ if the desired _path_ is not available in our `JsonElement`.\n\n\u003c!--- TEST --\u003e\n\n\u003c!--- INCLUDE\nfun main(): Unit {\n----- SUFFIX\n}\n--\u003e\n```kotlin\n  val json: JsonElement = Json.decodeFromString(JSON_STRING)\n  val name: Optional\u003cJsonElement, String\u003e = JsonPath.path(\"address.street.name\").string\n  val res: JsonElement = name.modify(json, String::uppercase).also(::println)\n  name.getOrNull(res)?.also(::println)\n```\n\u003e You can get the full code [here](src/jvmTest/kotlin/example/example-readme-02.kt).\n\n```text\n{\"name\":\"Arrow\",\"address\":{\"city\":\"Functional Town\",\"street\":{\"number\":1337,\"name\":\"FUNCTIONAL STREET\"}},\"employees\":[{\"name\":\"John\",\"lastName\":\"doe\"},{\"name\":\"Jane\",\"lastName\":\"doe\"}]}\nFUNCTIONAL STREET\n```\n\nIn the previous examples we've seen how we can select properties out of `JsonObject`,\nbut when working with `JsonElement` we also often have to deal with `JsonArray` that can contain many `JsonElement`.\nFor these use-cases we can use `every`, which focuses into _every_ `JsonElement` in our `JsonArray`.\n\nIn the example below we select the _employees_ `JsonArray`,\nand then we select _every_ `JsonElement` in the `JsonArray`.\nWe then _select_ the _name_ out of _every_ `JsonElement`.\n\nInstead of `Optional\u003cJsonElement, String\u003e` it now returns `Traversal\u003cJsonElement, String\u003e`,\nsince we selected _many properties_ instead of a _single property_.\n\nJust like before we can apply a function to it using _modify_,\nand we can also _extract every property_ using `getAll`.\nThis returns us an `emptyList()` when no values were found along the _path_,\nor all found values inside a `List`.\n\n\u003c!--- TEST --\u003e\n\n\u003c!--- INCLUDE\nfun main(): Unit {\n----- SUFFIX\n}\n--\u003e\n```kotlin\n  val json: JsonElement = Json.decodeFromString(JSON_STRING)\n  val employeesName: Traversal\u003cJsonElement, String\u003e = JsonPath.select(\"employees\").every.select(\"name\").string\n  val res: JsonElement = employeesName.modify(json, String::uppercase).also(::println)\n  employeesName.getAll(res).also(::println)\n```\n\u003e You can get the full code [here](src/jvmTest/kotlin/example/example-readme-03.kt).\n\n```text\n{\"name\":\"Arrow\",\"address\":{\"city\":\"Functional Town\",\"street\":{\"number\":1337,\"name\":\"Functional street\"}},\"employees\":[{\"name\":\"JOHN\",\"lastName\":\"doe\"},{\"name\":\"JANE\",\"lastName\":\"doe\"}]}\n[JOHN, JANE]\n```\n\nAlternatively we can also use the _pathEvery_ function to select _every_ `JsonElement` in our `JsonArray`.\nLike for _path_ we can also use the _dot (.) notation_ to select deeply nested properties.\nOn the other hand, the _star (*) notation_ allow us to select _every_ `JsonElement` in our `JsonArray`. \n\nLike before, below we select the _employees_ `JsonArray`,\nand then we select _every_ `JsonElement` in the `JsonArray`.\nWe then _select_ the _name_ out of _every_ `JsonElement`.\n\nAgain, instead of `Optional\u003cJsonElement, String\u003e` it now returns `Traversal\u003cJsonElement, String\u003e`,\nsince we selected _many properties_ instead of a _single property_.\n\nYou can then, apply a function to it using _modify_ like before.\n\n\u003c!--- TEST --\u003e\n\n\u003c!--- INCLUDE\nfun main(): Unit {\n----- SUFFIX\n}\n--\u003e\n```kotlin\n  val json: JsonElement = Json.decodeFromString(JSON_STRING)\n  val employeesName: Traversal\u003cJsonElement, String\u003e = JsonPath.pathEvery(\"employees.*.name\").string\n  val res: JsonElement = employeesName.modify(json, String::uppercase).also(::println)\n  employeesName.getAll(res).also(::println)\n```\n\u003e You can get the full code [here](src/jvmTest/kotlin/example/example-readme-04.kt).\n\n```text\n{\"name\":\"Arrow\",\"address\":{\"city\":\"Functional Town\",\"street\":{\"number\":1337,\"name\":\"Functional street\"}},\"employees\":[{\"name\":\"JOHN\",\"lastName\":\"doe\"},{\"name\":\"JANE\",\"lastName\":\"doe\"}]}\n[JOHN, JANE]\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnomisrev%2Fkotlinx-serialization-jsonpath","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnomisrev%2Fkotlinx-serialization-jsonpath","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnomisrev%2Fkotlinx-serialization-jsonpath/lists"}