Skip to content

Commit 690af38

Browse files
committed
Improved data.json.remove_keys() regex to cover keys that span multi-lines
1 parent a3086fd commit 690af38

2 files changed

Lines changed: 9 additions & 5 deletions

File tree

remove-json-keys/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "remove-json-keys"
7-
version = "0.0.19"
7+
version = "0.0.20"
88
description = "Remove key/value pairs from json_dir/**/*.json."
99
authors = [{name = "Adam Lui", email = "adam@kudoai.com"}]
1010
license = "MIT"

remove-json-keys/src/remove_json_keys/lib/data/json.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,18 @@ def remove_keys(cli):
1717
# Remove keys
1818
modified = False
1919
for key in cli.config.remove_keys:
20-
re_key = fr'"{re.escape(key)}".*?[,\n]+.*?(?="|$)'
21-
data, count = re.subn(re_key, '', data)
22-
if count > 0:
20+
re_key = fr'"{re.escape(key)}"\s*:\s*(?:\{{[^}}]*\}}|"[^"]*"|\d+|true|false|null)\s*,?\s*'
21+
data, cnt = re.subn(re_key, '', data)
22+
if cnt > 0:
2323
keys_removed.append((key, os.path.relpath(file_path, cli.config.json_dir)))
2424
modified = True
25-
else : keys_skipped.append((key, os.path.relpath(file_path, cli.config.json_dir)))
25+
else:
26+
keys_skipped.append((key, os.path.relpath(file_path, cli.config.json_dir)))
27+
28+
# Save modified JSON
2629
if modified:
2730
with open(file_path, 'w', encoding='utf-8') as f : f.write(data)
31+
2832
files_processed_cnt += 1
2933

3034
return keys_removed, keys_skipped, files_processed_cnt

0 commit comments

Comments
 (0)