Delete a line from json that matches regex in scala

Actual data:


How to remove the line from json file that matches regex for line.max and line.min?
Expected data:

This doesn’t look like legal JSON - the closing braces on each “line” are missing, and I’d expect a top level JSON array (or object) containing the “line” entries.

Assuming proper JSON, there’s no concept of a “line”. The formatting might be completely different without changing the actual structure/content, and your code should be prepared for this: Each object containing an “xyz” entry might spread over multiple lines, or the whole content might be cramped into a single line,…

I’d suggest to ensure that the input contains proper JSON, then use some JSON (object model) parser (spray-json, play-json,…) to convert the JSON input to a sequence of Scala objects, convert/filter at this level, and write back to JSON. If (and only if) this approach runs into performance issues, I’d look into other options, like streaming/event JSON parsers.

If the input really is like shown above and you have no way of forcing the upstream source to produce legal JSON, you may actually have to go with makeshift textual line level parsing. But this is going to be very sensitive against subtle changes to/variations in the input format - which is what structured formats like JSON and XML are supposed to guard you against.

1 Like