NodeJS: writing JSON file not having line breaks when i open in Notepad. But is pretty view with other Editors
9,850
The file most likely has UNIX line breaks (\n
), as opposed to Windows line breaks (\r\n
). Notepad is rather dumb and only supports the latter.
Wordpad, on the other hand, supports both. This is not a joke. ;)
Most “modern” editors support both and allow conversion between the two (and possibly Mac line breaks).
Related videos on Youtube
![Ƭᴇcʜιᴇ007](https://i.stack.imgur.com/CCjJd.png?s=256&g=1)
Author by
Ƭᴇcʜιᴇ007
Updated on September 18, 2022Comments
-
Ƭᴇcʜιᴇ007 almost 2 years
I am writing a JSON file using NodeJS, like this:
var path = "D:\\test.json" var writeContent = {"Success" : "This is a sample"} fs.writeFile(path, JSON.stringify(writeContent, null, 4), function (error) { if (error === null) { response.json(SuccessResponse); }else{ response.json(ErrorResponse + error.message); } });
The file is getting written successfully, and also the JSON file is pretty printed when I open the JSON file in latest file editors such as Subllime3, Notepad++ I am able to view the pretty form of the file. But when I open the same file in Notepad, I am not able to view the content in the pretty format.
It is viewed as:
{ "GlobalName": "CIRCULAR_GRATES_M01_METRIC", "LocalName": "Circular Grates M01" }
-
Admin almost 9 yearsRelated from SO: Node.JS constant for platform-specific new line?
-
-
Arjan almost 9 yearsSince 2001, Mac OS X line breaks are Unix line breaks,
0x0a
aka\n
akaLF
. (OS 9 and earlier used0x0d
aka\r
akaCR
.)