A Java library for dynamic JSON configuration parsing and mapping. This library provides utilities to generate configuration paths from JSON structures and map values between different JSON objects.
- JSON Path Generation: Generate configuration paths from JSON structures
- Dynamic JSON Mapping: Map values between different JSON objects using path expressions
- Array Support: Handle arrays and nested objects seamlessly
- Flexible Configuration: Support for both setter-style and simple path configurations
- Jackson Databind (included with Spring Boot)
- Java 21+
Use JsonToConfig.parseJson() to generate configuration paths from JSON:
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import static com.poliymorf.dynamicjsonconfig.mapper.JsonToConfig.*;
public class Example {
public static void main(String[] args) throws IOException {
String json = """
{
"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"}
]
}
}
}
""";
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(json);
// Generate setter-style paths
String setterPaths = parseJson(root, true);
System.out.println(setterPaths);
// Generate simple paths
String simplePaths = parseJson(root, false);
System.out.println(simplePaths);
}
}Output (setter-style):
& menu{}, id=
& menu{}, value=
& menu{}, popup{}, menuitem{}[], value=
& menu{}, popup{}, menuitem{}[], onclick=
Output (simple paths):
& menu{}, id
& menu{}, value
& menu{}, popup{}, menuitem{}[], value
& menu{}, popup{}, menuitem{}[], onclick
Use AutoRemap to map values between different JSON structures:
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.util.List;
import static com.poliymorf.dynamicjsonconfig.mapper.AutoRemap.*;
public class MappingExample {
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
// Source JSON
String sourceJson = """
{
"id": "0001",
"type": "donut",
"name": "Cake",
"batters": {
"batter": [
{"id": "1001", "type": "Regular"},
{"id": "1002", "type": "Chocolate"}
]
}
}
""";
// Target JSON template
String targetJson = """
{
"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"}
]
}
}
}
""";
JsonNode sourceNode = mapper.readTree(sourceJson);
ObjectNode targetNode = (ObjectNode) mapper.readTree(targetJson);
// Define mappings: target_path = source_path
String[] mappings = {
"& menu{}, id = & id",
"& menu{}, value = & type",
"& menu{}, popup{}, menuitem{}[], value = & batters{}, batter{}[], id",
"& menu{}, popup{}, menuitem{}[], onclick = & batters{}, batter{}[], type"
};
// Apply mappings
for (String mapping : mappings) {
String[] parts = mapping.split("=");
String targetPath = parts[0].trim();
String sourcePath = parts[1].trim();
List<JsonNode> values = getValuesByPath(sourceNode, sourcePath);
if (!values.isEmpty()) {
setValuesByPath(targetNode, targetPath, values);
}
}
// Print result
System.out.println(mapper.writerWithDefaultPrettyPrinter()
.writeValueAsString(targetNode));
}
}Output:
{
"menu": {
"id": "0001",
"value": "donut",
"popup": {
"menuitem": [
{
"value": "1001",
"onclick": "Regular"
},
{
"value": "1002",
"onclick": "Chocolate"
}
]
}
}
}The library uses a specific path syntax to navigate JSON structures:
{}- Navigate into an object[]- Navigate into an array{}[]- Navigate into an array within an object,- Separate path segments
& menu{}, id- Accessidfield withinmenuobject& items[]- Access arrayitems& menu{}, items{}[], name- Accessnamefield in array items within menu object
parseJson(JsonNode node, boolean setter)- Generate configuration paths from JSONnode: The JSON node to parsesetter: If true, generates setter-style paths with=; if false, generates simple paths
getValuesByPath(JsonNode node, String path)- Extract values from JSON using pathsetValuesByPath(ObjectNode node, String path, List<JsonNode> values)- Set values in JSON using path
mvn clean compilemvn exec:java -Dexec.mainClass="com.poliymorf.dynamicjsonconfig.example.ExampleCreateConfig"
mvn exec:java -Dexec.mainClass="com.poliymorf.dynamicjsonconfig.example.ExampleMapping"