-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform-field-names
executable file
·48 lines (39 loc) · 1.7 KB
/
transform-field-names
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python3
"""
Renames fields of the NDJSON record from stdin and outputs modified records
to stdout.
"""
import argparse
import json
from sys import stderr, stdin, stdout
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument("--field-map", nargs="+",
help="Fields names in the NDJSON record mapped to new field names, " +
"formatted as '{old_field_name}={new_field_name}'. " +
"If the old field does not exist in record, the new field will be added with an empty string value." +
"If the new field already exists in record, then the renaming of the old field will be skipped.")
parser.add_argument("--force", action="store_true",
help="Force renaming of old field even if the new field already exists. " +
"Please keep in mind this will overwrite the value of the new field.")
args = parser.parse_args()
field_map = {}
for field in args.field_map:
old_name, new_name = field.split('=')
field_map[old_name] = new_name
for record in stdin:
record = json.loads(record)
for old_field, new_field in field_map.items():
if record.get(new_field) and not args.force:
print(
f"WARNING: skipping rename of {old_field} because record",
f"already has a field named {new_field}.",
file=stderr
)
continue
record[new_field] = record.pop(old_field, '')
json.dump(record, stdout, allow_nan=False, indent=None, separators=',:')
print()