Skip to content

hidebike712/json-filter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JSON Filter

Overview

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.

Filter 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.

1. Inclusion Filter (FilterType.INCLUSION)

An inclusion filter extracts only the specified fields from the JSON structure, removing all others.

Example 1: Flat JSON Object

Filter:

"a,b"

Input JSON:

{"a":1,"b":2,"c":3}

Filtered Output:

{"a":1,"b":2}

Example 2: Nested JSON Object

Filter:

"x(y)"

Input JSON:

{"x":{"y":{"z":5},"w":10},"v":20}

Filtered Output:

{"x":{"y":{"z":5}}}

Example 3: Nested Array

Filter:

"name"

Input JSON:

[[[{"name":"john","type":0}]]]

Filtered Output:

[[[{"name":"john"}]]]

2. Exclusion Filter (FilterType.EXCLUSION)

An exclusion filter removes the specified fields while keeping all others.

Example 1: Flat JSON Object

Filter:

"a,b"

Input JSON:

{"a":1,"b":2,"c":3}

Filtered Output:

{"c":3}

Example 2: Nested JSON Object

Filter:

"x(y)"

Input JSON:

{"x":{"y":{"z":5},"w":10},"v":20}

Filtered Output:

{"x":{"w":10},"v":20}

Example 3: Nested Array

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.

Usage

Inclusion Filter

Example 1: Flat JSON Object

// 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);

Example 2: Nested JSON Object

// 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);

Example 3: Nested Array

// 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);

Exclusion Filter

Example 1: Flat JSON Object

// 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);

Example 2: Nested JSON Object

// 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);

Example 3: Nested Array

// 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);

Installation

<dependency>
    <groupId>org.czeal</groupId>
    <artifactId>json-filter</artifactId>
    <version>{version}</version>
</dependency>

License

Apache License, Version 2.0

Java Doc

JSON Filter JavaDoc

See Also

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages