- ignore any line which is exactly "[" or "]" - ignore any trailing commas
This lets you consume your API content both as a JSONL stream, and as a JSON array
not sure if it's that useful though
- allowing optionally enclosing the data into an array;
- allowing comma as an optional separator between objects;
- allowing JSON objects without a newline separator;
- properly escaping Unicode newlines and / characters inside string literals for JavaScript compatibility;
- allowing binary data inside string literals;
Recently (this year) I was flicking through a repo and saw a jsonl extension and immediately guessed what it would contain. It all just fits nicely.
/(\]|\})\n|(.")\n/
So in json you are simply putting in the magic character sequence: "\n" in place of the real newline character. Not unlike how many compilers will auto translate "\n" sequences you put in strings into the actual newline character.
I believe most json encoders will automatically convert newline characters into the two character sequence, so when you put it all together it goes: character sequence "\n" typed into source file -> compiler converts newline character into actual an actual newline character -> json encoder converts newline character back into "\n" sequence.
With all this in mind, since regex is basically doing the same thing as a compiler, treating "\n" sequences as a search for a newline character, you would need to escape the backslash so it is interpreted literally, so "\\n".
This gets even weirder depending on the tool/language you are doing this in though, like if you are writing out a regex within a string, the regex engine might not need to have any special logic for "\n" since the compiler is already handling it! In which case: "\\n" is an escape for the compiler not the regex engine.
TLDR: You just need to escape it: "\\n"
s = "foo\nbar\nbaz"
# 'foo\nbar\nbaz'
len(s) # 11
j = json.dumps(s)
# '"foo\\nbar\\nbaz"'
# \n is escaped as \\n
len(j) # 15[1] https://ecma-international.org/wp-content/uploads/ECMA-404_2...