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).

Share:
9,850

Related videos on Youtube

Ƭᴇcʜιᴇ007
Author by

Ƭᴇcʜιᴇ007

Updated on September 18, 2022

Comments

  • Ƭᴇcʜιᴇ007
    Ƭᴇ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"    }
    
  • Arjan
    Arjan almost 9 years
    Since 2001, Mac OS X line breaks are Unix line breaks, 0x0a aka \n aka LF. (OS 9 and earlier used 0x0d aka \r aka CR.)