I like the solutions proposed above because they are relatively simple and to-the-point. However, regex-replaces on JSON in general get pretty hairy due to factors including but not limited to:
how do you differentiate between a pattern in a key versus a string?
how do you take into account the fact that JSON doesn’t care about the order of keys whereas regexes do?
what if there are ] or } inside strings, such that the regex is fooled into thinking the JSON object ended?
Just to illustrate how annoying regex-replaces get once you start trying to satisfy all the syntactic requirements of JSON, here’s a regex-replace I came up with to achieve this while ignoring insignificant whitespace and the order of keys and removing any trailing commas:
find/replace (?-i),\s*{(?:[^{]*{[^}]*}\s*,\s*"model"\s*:\s*"[^"]*KeepInfo[^"]*"\s*|\s*"model"\s*:\s*"[^"]*KeepInfo[^"]*"[^{]*{[^}]*}\s*)}\s*(,)? with \1.
JsonTools is a fine solution for this, if you’re able to use plugins.
Because it uses a JSON parser to parse the JSON, it is insensitive to the formatting of the JSON.
To filter JSON with the plugin, just use Alt-P-J-J (tap the keys in sequence, don’t hold them down simultaneously) to open the tree view, then enter one of the following queries into the query box and hit Ctrl+Enter, then Save query result.
Two queries that accomplish your goals:
@[:][not(@.model =~ `(?i)keepinfo`)] filters out objects where the model includes keepinfo ignoring case.
@[:][not(@.model =~ KeepInfo)] filters out objects where the model includes KeepInfo in that case only.
While I’m here, PythonScript is also an efficient solution:
import json
from Npp import editor
j = json.loads(txt)
filtered = [o for o in j if 'KeepInfo' in o['model']]
filtered_text = json.dumps(filtered, indent=4)
editor.setText(filtered_text)