
Implements JSONPath to extract data from JSON structures using expressions. Supports RFC9535 specification, efficient processing for large documents, and includes extensive test coverage.
RJPath is JSONPath implementation for Kotlin Multiplatform that allows you to easily extract data from JSON structures using JSONPath expressions. The implementation follows the RFC9535 specification.
dependencies {
implementation("com.github.jershell:rjpath:1.1.1")
}dependencies {
implementation 'com.github.jershell:rjpath:1.1.1'
}import com.github.jershell.rjpath.RJPath
fun main() {
val jsonString = """
{
"store": {
"books": [
{
"title": "The Great Gatsby",
"price": 9.99
},
{
"title": "1984",
"price": 15.99
}
]
}
}
""".trimIndent()
val rjpath = RJPath.selector("$.store.books[*].title")
// Get all book titles
val titles = rjpath.getAll(json.parseToJsonElement(jsonString))
println(titles) // ["The Great Gatsby", "1984"]
// Get first book's price
val firstBookPrice = RJPath.selector("$.store.books[0].price")
.getFirst(json.parseToJsonElement(jsonString))
println(firstBookPrice) // 9.99
// Get all book titles with their paths
val titlesWithPaths = rjpath.getAllWithPath(json.parseToJsonElement(jsonString))
titlesWithPaths.forEach { (path, value) ->
println("$path: $value")
}
// Output:
// $.store.books[0].title: "The Great Gatsby"
// $.store.books[1].title: "1984"
}| Operator | Description | Example |
|---|---|---|
$ |
Root element | $ |
. |
Child operator | $.store.book |
.. |
Recursive descent | $..author |
* |
Wildcard | $.store.book[*] |
[start:end:step] |
Array slice | $[0:2] |
[,] |
Union | $[0,3] |
?() |
Filter (script) | $.store.book[?(@.price < 100)] |
For comprehensive examples and usage patterns, please refer to our test suite. The tests demonstrate various use cases including:
// Find all books over $10
val expensiveBooks = RJPath.selector("$.store.book[?(@.price > 10)]")
.getAll(jsonElement)
// Find books by specific author
val authorBooks = RJPath.selector("$.store.book[?(@.author == 'Herman Melville')]")
.getAll(jsonElement)// Find all prices in the document
val allPrices = RJPath.selector("$..price")
.getAll(jsonElement)
// Find all leaf values
val leaves = RJPath.selector("$..*")
.getAll(jsonElement)// Safe access with null handling
val maybeValue = RJPath.selector("$.nonexistent.path")
.getFirstOrNull(jsonElement)
// Get all matches or empty list
val allMatches = RJPath.selector("$.nonexistent.path")
.getAll(jsonElement) // returns empty list if no matchesWe welcome contributions! If you'd like to help:
git checkout -b feature/amazing-feature)git commit -m 'Add amazing feature')git push origin feature/amazing-feature)The library includes comprehensive test coverage. You can find test cases in:
These tests serve as both documentation and validation of the library's functionality.
Distributed under the MIT License. See LICENSE file for more information.
Special thanks to all contributors and community members.
RJPath is JSONPath implementation for Kotlin Multiplatform that allows you to easily extract data from JSON structures using JSONPath expressions. The implementation follows the RFC9535 specification.
dependencies {
implementation("com.github.jershell:rjpath:1.1.1")
}dependencies {
implementation 'com.github.jershell:rjpath:1.1.1'
}import com.github.jershell.rjpath.RJPath
fun main() {
val jsonString = """
{
"store": {
"books": [
{
"title": "The Great Gatsby",
"price": 9.99
},
{
"title": "1984",
"price": 15.99
}
]
}
}
""".trimIndent()
val rjpath = RJPath.selector("$.store.books[*].title")
// Get all book titles
val titles = rjpath.getAll(json.parseToJsonElement(jsonString))
println(titles) // ["The Great Gatsby", "1984"]
// Get first book's price
val firstBookPrice = RJPath.selector("$.store.books[0].price")
.getFirst(json.parseToJsonElement(jsonString))
println(firstBookPrice) // 9.99
// Get all book titles with their paths
val titlesWithPaths = rjpath.getAllWithPath(json.parseToJsonElement(jsonString))
titlesWithPaths.forEach { (path, value) ->
println("$path: $value")
}
// Output:
// $.store.books[0].title: "The Great Gatsby"
// $.store.books[1].title: "1984"
}| Operator | Description | Example |
|---|---|---|
$ |
Root element | $ |
. |
Child operator | $.store.book |
.. |
Recursive descent | $..author |
* |
Wildcard | $.store.book[*] |
[start:end:step] |
Array slice | $[0:2] |
[,] |
Union | $[0,3] |
?() |
Filter (script) | $.store.book[?(@.price < 100)] |
For comprehensive examples and usage patterns, please refer to our test suite. The tests demonstrate various use cases including:
// Find all books over $10
val expensiveBooks = RJPath.selector("$.store.book[?(@.price > 10)]")
.getAll(jsonElement)
// Find books by specific author
val authorBooks = RJPath.selector("$.store.book[?(@.author == 'Herman Melville')]")
.getAll(jsonElement)// Find all prices in the document
val allPrices = RJPath.selector("$..price")
.getAll(jsonElement)
// Find all leaf values
val leaves = RJPath.selector("$..*")
.getAll(jsonElement)// Safe access with null handling
val maybeValue = RJPath.selector("$.nonexistent.path")
.getFirstOrNull(jsonElement)
// Get all matches or empty list
val allMatches = RJPath.selector("$.nonexistent.path")
.getAll(jsonElement) // returns empty list if no matchesWe welcome contributions! If you'd like to help:
git checkout -b feature/amazing-feature)git commit -m 'Add amazing feature')git push origin feature/amazing-feature)The library includes comprehensive test coverage. You can find test cases in:
These tests serve as both documentation and validation of the library's functionality.
Distributed under the MIT License. See LICENSE file for more information.
Special thanks to all contributors and community members.