I'm writing some data files in JSON format and would like to have some really long string values split over multiple lines. Using python's JSON module I get a whole lot of errors, whether I use '\' or '\n' as an escape. Is it possible to have multi-line strings in JSON? It's mostly for visual comfort so I suppose I can just turn word wrap on in my editor, but I'm just kinda curious...

-1 The OP is using the "\n" escape sequence. It's not working because they're not escaping the backslash, as "\\n", so Python is converting the escape sequence to a newline character rather than leaving it as literally a backslash followed by an en, as JSON requires.
–
user359996Jan 30 '13 at 21:57

1

@user359996 I'm not sure that's true. For me (storing data in JSON with just \n and outputting it via Curses), \n seems to work okay. It depends on the view/rendering engine, it seems.
–
ashes999Mar 7 '14 at 14:28

1

Newline sequences are indeed platform-specific (cf. en.wikipedia.org/wiki/Newline#Representations). However, per @Lightness Races in Orbit's answer, neither cursor-return nor line-feed characters are in the JSON grammar. I'm actually not a Python programmer, so I'm not sure what's going on in your case, but either your parser is broken or you're not passing it what you think your are. Maybe this answer explains it better: stackoverflow.com/a/9295597/359996. Note especially the bit about double escaping.
–
user359996Mar 7 '14 at 17:28

This looks quite neat to me, appart from that I have to use double quotes everywhere. Though otherwise, I could, perhaps, use YAML, but that has other pitfalls and is not supported natively. Once parsed, I just use myData.modify_head.join('\n').

This is a solution for a specific setting, not necessarily related to the question. What you create there are not multiline strings (which is not possible anyway), but arrays with strings inside
–
Samuel RivasJun 12 at 13:04

This shows how to insert newline in strings, which does NOT answer the question. This answer does.
–
fgrieuJul 16 at 7:20

This is a really old question, but I came across this on a search and I think I know the source of your problem.

JSON does not allow "real" newlines in its data; it can only have escaped newlines. See the answer from @YOU, above. According to the question, it looks like you attempted to escape line breaks in Python two ways: by using the line continuation character ("\") or by using "\n" as an escape.

But keep in mind: if you are using a string in python, special escaped characters ("\t", "\n") are translated into REAL control characters! The "\n" will be replaced with the ASCII control character representing a newline character, which is precisely the character that is illegal in JSON. (As for the line continuation character, it simply takes the newline out.)

So what you need to do is to prevent Python from escaping characters. You can do this by using a raw string (put r in front of the string, as in r"abc\ndef", or by including an extra slash in front of the newline ("abc\\ndef").

Both of the above will, instead of replacing "\n" with the real newline ASCII control character, will leave "\n" as two literal characters, which then JSON can interpret as a newline escape.