This project provides a set of utilities for building, manipulating, and validating JSON objects in Java. It includes classes for creating JSON objects from various sources, updating JSON nodes, transforming JSON nodes to POJOs, and validating JSON against a set of rules.
To include this project in your Maven build, add the following dependency to your pom.xml:
<dependency>
<groupId>org.json.builder</groupId>
<artifactId>json-builder</artifactId>
<version>1.0.4</version>
</dependency>Important
Always check for the latest version at Maven Central Repository.
- Java: JDK 17 or higher
- Maven: 3.6.3 or higher
JsonObjectBuilder is used to create and manipulate JSON objects.
JsonObjectBuilder builder = JsonBuilder.objectBuilder();
builder.fromJsonString("{\"name\":\"John\"}")
.update("age", 30)
.build();
String jsonString = builder.toPrettyString();
System.out.println(jsonString);Build JSON data by reading from a JSON file.
JsonObjectBuilder builder = JsonBuilder.objectBuilder();
String jsonFileName = "file-path/data.json";
String json = builder.fromJsonFile(jsonFileName).build().toPrettyString();
System.out.println(json);Build JSON data by reading from a JSON file.
JsonObjectBuilder builder = JsonBuilder.objectBuilder();
File jsonFile = new File("file-path/data.json");
String json = builder.fromJsonFile(jsonFile).build().toPrettyString();
System.out.println(json);Build JSON data by reading from a JSON string.
JsonObjectBuilder builder = JsonBuilder.objectBuilder();
String json = builder.fromJsonString("{\"name\":\"John\"}").build().toPrettyString();
System.out.println(json);Creates an empty JSON object.
JsonObjectBuilder builder = JsonBuilder.objectBuilder();
String json = builder.fromEmptyNode()
.update("name", "John")
.update("age", 30)
.build()
.toPrettyString();
System.out.println(json);Updates the value at the specified JSON node path with the given value and data type.
JsonObjectBuilder builder = JsonBuilder.objectBuilder();
builder.fromJsonString("{\"name\":\"John\"}")
.update("age", 30, NodeType.INT)
.build();
String jsonString = builder.toPrettyString();
System.out.println(jsonString);Updates the value at the specified JSON node path with the given value as a string.
JsonObjectBuilder builder = JsonBuilder.objectBuilder();
builder.fromJsonString("{\"name\":\"John\"}")
.update("age", 30)
.build();
String jsonString = builder.toPrettyString();
System.out.println(jsonString);Updates the value of a key in an array node if the specified condition is met.
JsonObjectBuilder builder = JsonBuilder.objectBuilder();
Predicate<JsonNode> condition = node -> node.get("name").asText().equals("John");
builder.fromJsonString("[{\"name\":\"John\", \"age\":25}, {\"name\":\"Jane\", \"age\":30}]")
.updateArrayNodeIf(condition, "", "age", "26")
.build();
String jsonString = builder.toPrettyString();
System.out.println(jsonString);Removes the JSON node at the specified path.
JsonObjectBuilder builder = JsonBuilder.objectBuilder();
builder.fromJsonString("{\"name\":\"John\", \"age\":30}")
.remove("age")
.build();
String jsonString = builder.toPrettyString();
System.out.println(jsonString);Builds the JSON object by applying all the updates and removals.
JsonObjectBuilder builder = JsonBuilder.objectBuilder();
builder.fromJsonString("{\"name\":\"John\", \"age\":30}")
.update("age", 31)
.remove("name")
.build();
String jsonString = builder.toPrettyString();
System.out.println(jsonString);Important
Converts the JSON object to a pretty-printed string.
JsonObjectBuilder builder = JsonBuilder.objectBuilder();
builder.fromJsonString("{\"name\":\"John\", \"age\":30}")
.update("age", 31)
.remove("name")
.build();
String jsonString = builder.toPrettyString();
System.out.println(jsonString);Builds the JSON object as a JsonNode by applying all the updates and removals.
JsonObjectBuilder builder = JsonBuilder.objectBuilder();
builder.fromJsonString("{\"name\":\"John\", \"age\":30}")
.update("age", 31)
.remove("name")
.buildAsJsonNode();
JsonNode jsonNode = builder.buildAsJsonNode();
System.out.println(jsonNode.toPrettyString());Retrieves the JSON node at the specified path.
JsonObjectBuilder builder = JsonBuilder.objectBuilder();
builder.fromJsonString("{\"name\":\"John\", \"age\":30}");
JsonNode node = builder.getNodeAt("name");
System.out.println(node.asText());Important
Ensure the JSON path always follows '.' separated path. For example, address[0].city.
Cleans the JSON object builder by removing all nodes and clearing the maps of paths to append and remove.
JsonObjectBuilder builder = JsonBuilder.objectBuilder();
builder.fromJsonString("{\"name\":\"John\", \"age\":30}")
.update("age", 31)
.clean();
String jsonString = builder.toPrettyString();
System.out.println(jsonString);Writes the JSON object to a file at the specified file path.
JsonObjectBuilder builder = JsonBuilder.objectBuilder();
builder.fromJsonString("{\"name\":\"John\", \"age\":30}")
.update("age", 31)
.remove("name")
.writeTo("output.json");Checks if the JSON object builder is empty.
JsonObjectBuilder builder = JsonBuilder.objectBuilder();
boolean isEmpty = builder.isBuilderEmpty();
System.out.println(isEmpty);Transforms the root JSON object node to a POJO of the specified class type.
JsonObjectBuilder builder = JsonBuilder.objectBuilder();
builder.fromJsonString("{\"name\":\"John\", \"age\":30}");
Person person = builder.transformToPojo(Person.class);
System.out.println(person.getName());
System.out.println(person.getAge());Transforms the JSON node at the specified path to a POJO of the specified class type.
JsonObjectBuilder builder = JsonBuilder.objectBuilder();
builder.fromJsonString("{\"person\":{\"name\":\"John\", \"age\":30}}");
Person person = builder.transformNodeToPojo("person", Person.class);
System.out.println(person.getName());
System.out.println(person.getAge());Extracts all JSON paths from the root JSON object node.
JsonObjectBuilder builder = JsonBuilder.objectBuilder();
builder.fromJsonString("{\"name\":\"John\", \"address\":{\"city\":\"New York\"}}");
List<String> paths = builder.extractJsonPaths();
paths.forEach(System.out::println);Extracts a map of JSON paths and their corresponding values from the root JSON object node.
JsonObjectBuilder builder = JsonBuilder.objectBuilder();
builder.fromJsonString("{\"name\":\"John\", \"address\":{\"city\":\"New York\"}}");
Map<String, String> pathValueMap = builder.extractJsonPathValueMap();
pathValueMap.forEach((path, value) -> System.out.println(path + ": " + value));JsonArrayBuilder also has the same usages as JsonObjectBuilder. The methods and their usage examples are similar, but they operate on JSON arrays instead of JSON objects.
JsonValidator is used to validate JSON against a set of rules defined in a rule book.
List<String> validationErrors = JsonValidator.verify("rule-book.json", "actual-json.json");
if (validationErrors.isEmpty()) {
System.out.println("All validations passed.");
} else {
validationErrors.forEach(System.out::println);
}JsonPathFinder is used to find JSON paths in a JSON object. Finds the JSON path of a sub-node within a root node.
JsonNode rootNode = ...; // Initialize root node
JsonNode subNode = ...; // Initialize sub node
String jsonPath = JsonPathFinder.getJsonPath(rootNode, subNode);
System.out.println(jsonPath);This project is licensed under the MIT License. See the LICENSE file for details.
For any questions or issues, please contact:
- Name: Rajkumar Rajamani
- Email: rajoviyaa.s@gmail.com