Skip to content

s4kibs4mi/kotlin-jsonq

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

kotlin-jsonq

License: CC0-1.0

A simple Kotlin library to Query over JSON Data

Installation

Gradle,

allprojects {
    repositories {
        ...
        maven { url "https://jitpack.io" }
    }
}

And

dependencies {
    implementation 'ninja.sakib:kotlin-jsonq:v0.2'
}

Usage

Let's see a quick example:

Sample data (data.json) ```json { "name": "products", "description": "Features product list", "vendor": { "name": "Computer Source BD", "email": "info@example.com", "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 ] } ```

Examples

  • 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"
  }
]

TODO

  • Add missing methods
  • Write full documentation with example

Bugs and Issues

If you encounter any bugs or issues, feel free to open an issue at github.

Also, you can shoot me an email to mailto:root@sakib.ninja for hugs or bugs.

Special Mention

Nahid Bin Azhar for the original idea.

Contribution

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

See all contributors