2

I'm able to remove the key from JsonObject using:

String prop = "test";
JsonObject o = parser.parse(props).getAsJsonObject();
o.remove(prop);

I need to remove all the keys matching certain pattern from the JsonObject, like anything that starts with "test.*". Instead of iterating over the keys and find a match, is there any other way to remove the keys matching a given pattern?

input: {"test":"0","test_1": "1","test_10":"10", "site":"abc.com"}
expected output: {"site":"abc.com"}

Thanks!

2
  • can you share a input / output example please Commented Dec 7, 2017 at 19:26
  • iterate over keys and check for contains your test string Commented Dec 7, 2017 at 20:07

1 Answer 1

1

There isn't.

The best you can do is compile the regex before the loop to improve performance:

Pattern pattern = Pattern.compile(prop);
Iterator<Entry<...>> it = o.entrySet().iterator();
while (it.hasNext()) {
    Entry<...> entry = it.next();
    if (pattern.matcher(entry.getKey()).matches()) {
        i.remove();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.