I cannot use a standard JSON parser because I'm working with .jbeam files, which are based on JSON but follow a custom format used internally by BeamNG. These files contain unique formatting rules that cause standard JSON parsers to fail, such as support for comments and the omission of commas between elements, which would typically result in errors in a regular JSON parser. The task I'm attempting to accomplish is relatively simple: replace the nodes property with some new text in a specific format, while preserving the overall formatting of the original file (which is quite large, though I’m only using a simplified example here). However, using a standard JSON parser alters the formatting of other properties in the file as well. I attempted the solution outlined in this thread, but it quickly became cumbersome, as I had to wrap everything to preserve the formatting of the file, which doesn’t make sense for such a straightforward task. The original formatting is important because without it you will have a nightmare reading .jbeam files as they would increase in number of lines, dramatically decreasing readability. After hours of struggling with various methods, I ultimately decided to take the simplest approach by using basic text replacement. However, even for simple operations like removing text, I'm encountering issues. I know this task could be easily handled with a bash script, but I’m curious why it's proving to be so difficult with Python. How can I efficiently remove the contents of the nodes property in this example using Python? sample_jbeam = ''' { "partname": { "refNodes": [ ["ref:", "back:", "left:", "up:", "leftCorner:", "rightCorner:"], ["ref", "", "", "", "", ""] ], "nodes": [ ["id", "posX", "posY", "posZ"], ["ref", 0, 0, 0], ["b1", 1.0, 1.0, 1.0], ], "beams": [ ], } } ''' I want to remove the contents inside "nodes" so I that I'm left with: { "partname": { "refNodes": [ ["ref:", "back:", "left:", "up:", "leftCorner:", "rightCorner:"], ["ref", "", "", "", "", ""] ], "nodes": [], "beams": [ ], } } Since I cannot use the Python's json loader because jbeam is not really json, and besides json will change the formatting of the original file. Failed Attempt 1: RegEx I tried RegEx but it doesn't work: pattern = r'("nodes":\s*)\[.*?\]' # cleaned_text = re.sub(pattern, r'\1[]', sample_jbeam, flags=re.DOTALL) cleaned_text = re.sub(r'"nodes": \[.*?\]', '"nodes": []', text, flags=re.DOTALL) print(cleaned_text) Failed Attempt 2: io.StringIO I've tried using io.StringIO() as in my answer post but after I changed the jbeam formatting it stopped working. Failed Attempt 3: json Preprocessor I tried using this Amazing Json preprocessor which works really well in making the jbeam valid json but unfortunately the original formatting is also changed. I don't know why this question is not well received by the community but I feel that this is a legit problem to which I cannot find any solutions. Continue reading...