A simple Kotlin library to Query over JSON Data
Gradle,
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
And
dependencies {
implementation 'ninja.sakib:kotlin-jsonq:v0.2'
}
Let's see a quick example:
Sample data (data.json)
```json { "name": "products", "description": "Features product list", "vendor": { "name": "Computer Source BD", "email": "[email protected]", "website": "www.example.com" }, "users": [ { "id": 1, "name": "Johura Akter Sumi", "location": "Barisal" }, { "id": 2, "name": "Mehedi Hasan Nahid", "location": "Barisal" }, { "id": 3, "name": "Ariful Islam", "location": "Barishal" }, { "id": 4, "name": "Suhel Ahmed", "location": "Sylhet" }, { "id": 5, "name": "Firoz Serniabat", "location": "Gournodi" }, { "id": 5, "name": "Musa Jewel", "location": "Barishal", "visits": [ { "name": "Sylhet", "year": 2011 }, { "name": "Cox's Bazar", "year": 2012 }, { "name": "Bandarbar", "year": 2014 } ] } ], "products": [ { "id": 1, "city": "bsl", "name": "iPhone", "cat": 1, "price": 80000.5 }, { "id": 2, "city": null, "name": "macbook pro", "cat": 2, "price": 150000.1 }, { "id": 3, "city": "dhk", "name": "Redmi 3S Prime", "cat": 1, "price": 12000.1 }, { "id": 4, "city": null, "name": "Redmi 4X", "cat": 1, "price": 15000.1 }, { "id": 5, "city": "bsl", "name": "macbook air", "cat": 2, "price": 110000.00 }, { "id": 6, "city": null, "name": "macbook air 1", "cat": 2, "price": 81000.2 } ], "cities": [ { "id": 1, "name": "Barishal" }, { "id": 2, "name": "Noakhali" }, { "id": 3, "name": "Dhaka" }, { "id": 4, "name": "Rajshahi" }, { "id": 5, "name": "Chittagong" } ], "arr": [ 1, 2, 3, 4 ] } ```- Initialization,
val inputStream = Thread.currentThread().contextClassLoader.getResourceAsStream("data.json")
jsonq = JSONQ(inputStream)
- find,
val obj = jsonq.find("users.5.visits")
Result,
[
{
"name": "Sylhet",
"year": 2011
},
{
"name": "Cox's Bazar",
"year": 2012
},
{
"name": "Bandarbar",
"year": 2014
}
]
- whereEq,
val res = jsonq.from("users.5.visits").whereEq("name", "Bandarbar")
Result,
[
{
"name": "Bandarbar",
"year": 2014
}
]
- hasSuffix,
val res = jsonq.from("users").hasSuffix("name", "Sumi")
Result,
[
{
"id": 1,
"name": "Johura Akter Sumi",
"location": "Barisal"
}
]
- Query Chaining
val res = jsonq.from("users").whereGe("id", 3).whereEq("location", "Barisal").contains("name", "Is")
Result,
[
{
"id": 3,
"name": "Ariful Islam",
"location": "Barisal"
}
]
- Add missing methods
- Write full documentation with example
If you encounter any bugs or issues, feel free to open an issue at github.
Also, you can shoot me an email to mailto:[email protected] for hugs or bugs.
Nahid Bin Azhar for the original idea.
If you are interested to make the package better please send pull requests or create an issue so that others can fix. Read the contribution guide here