top of page
Search

JsonDot: Write JSON Code That Reads Like a Thought

Updated: Apr 27

Have you ever looked at your Java code and thought: “There’s no way all this just to get one field out of a JSON object makes sense.”

If you've battled with long chains of getJSONObject().getJSONArray() and wondered if there's a cleaner way — you're not imagining things. Java’s default way of working with JSON can feel like wrestling a machine just to say something simple.


That’s why JsonDot — a lightweight Java library that brings back joy and elegance to JSON handling. With dot-notation path access, deep merge, XML conversion, and smart array querying, you can focus on logic, not on boilerplate.

It’s like giving your Java code a breath of fresh air.


What is JsonDot?


JsonDot is a modern Java library that simplifies working with JSON. It allows intuitive dot-notation access to deeply nested fields, smartly handles arrays, supports XML conversion, and includes helpful utilities like path validation and deep merging.

It's built for real-world use cases like configuration management, API response parsing, and data transformation — with performance and readability in mind.


🔑 Key Features


1. Dot Notation Path Access

Access deeply nested fields with just one line:


JsonDot jsonDot = new JsonDot(); 
JSONObject json = new JSONObject("{\"user\":{\"name\":\"John\"}}"); 
String name = jsonDot.get(json, "user.name", String.class);

2. Array Support

  • Index-based: users[0].name

  • Wildcard: users[*].name

  • Filter and map arrays using predicates


3. Utility Methods


✅ Deep Merge

Merge two JSON objects without overwriting nested structures:

JSONObject target = new JSONObject("{\"user\":{\"name\":\"John\",\"age\":30}}"); 
JSONObject source = new JSONObject("{\"user\":{\"age\":31,\"city\":\"NY\"}}"); 
JSONObject merged = JsonUtils.deepMerge(target, source);

🔄 XML Conversion
// JSON to XML 
String xml = JsonUtils.toXml(json); 

// XML to JSON 
JSONObject json = JsonUtils.fromXml(xml);

🔍 JSON Validation
boolean isValid = JsonUtils.isValidJson("{\"key\":\"value\"}");

💡 Common Use Cases

API Response Handling

JsonDot response = new JsonDot(apiResponse);
List<Object> items = response.query("data.items.*");

Data Transformation


JsonDot source = new JsonDot(sourceData); 
JsonDot target = new JsonDot(); 
target.addElement("user.name", source.getString("name")) .addElement("user.age", source.getInt("age"));

🔍 Advanced Features

Path Validation

if (jsonDot.isValidPath("user.address.city")){ 
String city = jsonDot.get(json, "user.address.city", String.class); 
}

Array Filtering

List<JSONObject> filtered = jsonDot.filterArray( json, "products", product -> product.getInt("price") > 100 );

Type-Safe Accessors

String name = jsonDot.getAsString(json, "user.name"); 
int age = jsonDot.getAsInt(json, "user.age"); 
boolean active = jsonDot.getAsBoolean(json, "user.active");

✅ Best Practices

  • Use isValidPath to avoid runtime path errors

  • Prefer type-safe methods for predictable casting

  • Handle JsonDotException for invalid operations

  • Cache common paths if performance is critical

  • Use JsonArrayDot for array-heavy workflows


⚙️ Performance Considerations

  • Memory-efficient and fast

  • Optimized for repeated access

  • Comparable speed with other popular libraries

  • No reflection, minimal dependencies


📦 Getting Started

Add this to your pom.xml:

<dependency> 
<groupId>tech.coderscamp.jsondot</groupId> <artifactId>jsondot</artifactId> 
<version>1.0.0</version> 
</dependency>

🤝 Why Use JsonDot?


JsonDot was built out of practical frustrations and real engineering needs. It's not just a wrapper—it's a rethinking of how we should work with JSON in Java:

  • Clean, expressive code

  • Fewer lines, fewer bugs

  • Powerful features when you need them

  • Minimal setup — ready to use in minutes

If you're building anything in Java that touches JSON — whether it's a microservice, a CLI tool, or a large backend system — JsonDot is here to make your life easier.


✨ JsonDot: Supported Methods

Here’s a quick overview of all the powerful methods available in JsonDot:


Constructor Methods
  • JsonDot() — Creates an empty JSON object.

  • JsonDot(String jsonString) — Creates a JsonDot from a JSON string.

  • JsonDot(JSONObject jsonObject) — Creates a JsonDot from an existing JSONObject.


Path Validation
  • isValidPath(String path) — Validates if a dot-notation path is correctly formatted.


Element Manipulation
  • addElement(String path, Object value) — Adds a value at the specified path.

  • removeElementFromPath(String path) — Removes an element at a specific path.

  • removeElementFromAllPaths(String path) — Removes elements at all matching paths.

  • addElementToArray(String path, String key, Object value) — Adds a key-value pair to all objects in an array.

  • addElementToAllPaths(String path, Object value) — Adds a value to all matching paths.

  • addElementAtPath(String path, Object value) — Adds a value at a specific path.


Element Retrieval
  • get(String path) — Retrieves the raw value at a path.

  • getString(String path) — Retrieves a String value at a path.

  • getInt(String path) — Retrieves an Integer value at a path.

  • getDouble(String path) — Retrieves a Double value at a path.

  • getBoolean(String path) — Retrieves a Boolean value at a path.

  • getObject(String path) — Retrieves a JsonDot object at a path.

  • getArray(String path) — Retrieves a JsonArrayDot at a path.

  • getJSONObject(String path) — Retrieves the raw JSONObject at a path.

  • getJSONArray(String path) — Retrieves the raw JSONArray at a path.


Path Operations
  • hasPath(String path) — Checks if a path exists.

  • getKeys() — Gets all top-level keys.

  • suggestPaths(String partialPath) — Suggests possible path completions based on input.

  • findMatchingPaths(String path) — Finds all paths matching a pattern.

  • query(String pattern) — Queries elements matching a pattern.


Array Operations
  • filterArray(String path, Predicate<Object> condition) — Filters array elements based on a condition.


Format Conversion
  • toString() — Converts the object into a JSON string.

  • toPrettyString() — Converts the object into a pretty-formatted JSON string.

  • toXml() — Converts the JSON into XML.

  • toPrettyXml() — Converts the JSON into formatted XML.

  • pathToXml(String path) — Converts a specific path's data into XML.


Comparison
  • diff(JsonDot other) — Compares with another JsonDot object and returns differences.


Utility Methods
  • isNull(String path) — Checks if the value at the given path is null.


🔗 Learn More

You can find the code, documentation, and usage examples on GitHub.


Thank you for joining me on this journey with JsonDot! I’d love to hear your thoughts, ideas, or ways you plan to use it.

If JsonDot made your work a little easier or inspired you in any way, that’s the greatest reward. Stay tuned for more updates!

 
 
 

Recent Posts

See All

Comments


bottom of page