Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scrubber for JSON paths #598

Open
mkutz opened this issue Dec 4, 2024 · 3 comments
Open

Scrubber for JSON paths #598

mkutz opened this issue Dec 4, 2024 · 3 comments

Comments

@mkutz
Copy link

mkutz commented Dec 4, 2024

I want to be able to scrub specific fields in a JSON document.

E.g. when I test a POST API call and the resulting JSON contains multiple UUIDs. The top level UUID has been dynamically generated by the backend and should be scrubbed. However, another UUID is a reference that was contained in my request and hence should not be scrubbed.

I'd like support for such cases with a Scrubber implementation that allows to scrub specific JSON fields using JSON path.

@mkutz
Copy link
Author

mkutz commented Dec 4, 2024

Here's one possible implementation.

class JsonPathScrubber implements Scrubber {

  private final JsonPath path;
  private final Configuration configuration;

  public JsonPathScrubber(JsonPath path, Configuration configuration) {
    this.path = path;
    this.configuration = configuration;
  }

  public JsonPathScrubber(JsonPath path) {
    this(path, Configuration.builder().build());
  }

  @Override
  public String scrub(String json) {
    DocumentContext documentContext = JsonPath.parse(json);
    String finding = documentContext.read(path);
    return json.replace(finding, "[scrubbed]");
  }
}

Note that this does only work for JSON paths that return a single value. I can probably find a solution that would also allow to scrub paths that return lists.

I first tried to use JSON path's set method, but its jsonString method would return a minified JSON, hence I decided to use the found value and replace it. This works well, as long as the found value is something like a UUID, but will cause unwanted side effects if it was a less unique value like true or 1.

Also this implementation adds a new dependency to JSON path.

@LarsEckart
Copy link
Contributor

hey Michael,

we get together again next Monday, if you're interested and want to present your issue, and potentially then immediately mob program the solution with us, I can send you a calendar invite. It's 18 o'clock German time. Otherwsie, we'll also take a look at this issue without you ;)

@LarsEckart
Copy link
Contributor

Sorry, we didn't get to it today, we'll check in again February 3rd.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants