A lightweight helper/wrapper library for org.json
to safely extract values from JSONObject
and JSONArray
without repetitive null checks.
Add the dependency to your project:
Maven
<dependency>
<groupId>io.github.raghavendar-ts</groupId>
<artifactId>jsonutil-orgjson</artifactId>
<version>1.0.0</version>
</dependency>
implementation 'io.github.raghavendar-ts:jsonutil-orgjson:1.0.0'
implementation("io.github.raghavendar-ts:jsonutil-orgjson:1.0.0")
import org.json.JSONObject;
import io.github.raghavendarts.jsonutil.JSONUtil;
public class Example {
public static void main(String[] args) {
JSONObject obj = new JSONObject();
obj.put("id", 101);
obj.put("name", "Raghavendar");
obj.put("active", true);
// Safe extraction
Integer id = JSONUtil.getInteger(obj, "id"); // 101
String name = JSONUtil.getString(obj, "name"); // "Raghavendar"
Boolean active = JSONUtil.getBoolean(obj, "active");// true
String email = JSONUtil.getString(obj, "email"); // "" (default safe value)
System.out.println(id + ", " + name + ", " + active + ", " + email);
}
}
- Avoids repetitive null and JSONException checks.
- Provides safe defaults ("", 0, false, empty JSONObject/JSONArray).
- Works as a wrapper on top of the popular org.json library.
This project is licensed under the MIT License — see the LICENSE file for details.
Raghavendar T S
GitHub: @raghavendar-ts
LinkedIn: Raghavendar T S