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.
- 🚀 JSONPath RFC9535 specification support
- 💻 Kotlin Multiplatform (JVM, JS, Native, Wasm, IOS)
- 🧪 Test coverage
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 matches
We welcome contributions! If you'd like to help:
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to your fork (
git push origin feature/amazing-feature
) - Open a Pull Request
The library includes comprehensive test coverage. You can find test cases in:
- JsonPathTest.kt - Core functionality tests
- JsonPathFunctionsTest.kt - Function tests
These tests serve as both documentation and validation of the library's functionality.
Distributed under the MIT License. See LICENSE
file for more information.
- GitHub: jershell
- Email: tukhvatullin01@gmail.com
Special thanks to all contributors and community members.