This library provides a flexible way to filter Gson's JsonElement objects by selectively including or excluding specific nodes. With support for nested filtering, it provides precise control over JSON structures using an intuitive and concise syntax.
This library provides a simple yet powerful syntax for filtering JSON elements using inclusion and exclusion rules. The filtering syntax allows selecting or removing specific fields, including support for nested structures and arrays.
An inclusion filter extracts only the specified fields from the JSON structure, removing all others.
Filter:
"a,b"
Input JSON:
{"a":1,"b":2,"c":3}
Filtered Output:
{"a":1,"b":2}
Filter:
"x(y)"
Input JSON:
{"x":{"y":{"z":5},"w":10},"v":20}
Filtered Output:
{"x":{"y":{"z":5}}}
Filter:
"name"
Input JSON:
[[[{"name":"john","type":0}]]]
Filtered Output:
[[[{"name":"john"}]]]
An exclusion filter removes the specified fields while keeping all others.
Filter:
"a,b"
Input JSON:
{"a":1,"b":2,"c":3}
Filtered Output:
{"c":3}
Filter:
"x(y)"
Input JSON:
{"x":{"y":{"z":5},"w":10},"v":20}
Filtered Output:
{"x":{"w":10},"v":20}
Filter:
"name"
Input JSON:
[[[{"name":"john","type":0}]]]
Filtered Output:
[[[{"type":0}]]]
This syntax allows precise control over JSON structures using simple and readable filtering rules.
// Parse a JSON string using JsonParser into a JsonElement instance.
JsonElement jsonElement = JsonParser.parseString("{\"a\":1,\"b\":2,\"c\":3}");
// Create an inclusion filter.
Filter filter = new FilterFactory().create(FilterType.INCLUSION);
// Apply the filter with "a,b" to the JsonElement instance.
JsonElement filtered = filter.apply(jsonElement, "a,b");
// {"a":1,"b":2}
System.out.println(filtered);
// Parse a JSON string using JsonParser into a JsonElement instance.
JsonElement jsonElement = JsonParser.parseString("{\"x\":{\"y\":{\"z\":5},\"w\":10},\"v\":20}");
// Create an inclusion filter.
Filter filter = new FilterFactory().create(FilterType.INCLUSION);
// Apply the filter with "x(y)" to the JsonElement instance.
JsonElement filtered = filter.apply(jsonElement, "x(y)");
// {\"x\":{\"y\":{\"z\":5}}}
System.out.println(filtered);
// Parse a JSON string using JsonParser into a JsonElement instance.
JsonElement jsonElement = JsonParser.parseString("[[[{\"name\":\"john\",\"type\":0}]]]");
// Create an inclusion filter.
Filter filter = new FilterFactory().create(FilterType.INCLUSION);
// Apply the filter with "name" to the JsonElement instance.
JsonElement filtered = filter.apply(jsonElement, "name");
// [[[{"name":"john"}]]]
System.out.println(filtered);
// Parse a JSON string using JsonParser into a JsonElement instance.
JsonElement jsonElement = JsonParser.parseString("{\"a\":1,\"b\":2,\"c\":3}");
// Create an exclusion filter.
Filter filter = new FilterFactory().create(FilterType.EXCLUSION);
// Apply the filter with "a,b" to the JsonElement instance.
JsonElement filtered = filter.apply(jsonElement, "a,b");
// {"c":3}
System.out.println(filtered);
// Parse a JSON string using JsonParser into a JsonElement instance.
JsonElement jsonElement = JsonParser.parseString("{\"x\":{\"y\":{\"z\":5},\"w\":10},\"v\":20}");
// Create an exclusion filter.
Filter filter = new FilterFactory().create(FilterType.EXCLUSION);
// Apply the filter with "x(y)" to the JsonElement instance.
JsonElement filtered = filter.apply(jsonElement, "x(y)");
// {"x":{"w":10},"v":20}
System.out.println(filtered);
// Parse a JSON string using JsonParser into a JsonElement instance.
JsonElement jsonElement = JsonParser.parseString("[[[{\"name\":\"john\",\"type\":0}]]]");
// Create an exclusion filter.
Filter filter = new FilterFactory().create(FilterType.EXCLUSION);
// Apply the filter with "name" to the JsonElement instance.
JsonElement filtered = filter.apply(jsonElement, "name");
// [[[{"type":0}]]]
System.out.println(filtered);
<dependency>
<groupId>org.czeal</groupId>
<artifactId>json-filter</artifactId>
<version>{version}</version>
</dependency>
Apache License, Version 2.0