giuseppebrb
giuseppebrb

Reputation: 85

On Azure I have JSON responses with extra spaces after \n

I've created some web-api using ASP.NET Core 2.2 that return me JSON responses.

However in my local debug, when I make a call, everything seems ok but when I proceed publishing on Azure in my JSON response I see extra spaces after every \n I inserted during the seeding process.

So for the same request I have this local response:

{"id":57,"content":"First Line. \nSecond Line.\nThird Line","otherAttrubute":"otherValue"}

And this live (or remote if you prefer) version:

{"id":57,"content":"First Line. \r\n    Second Line.\r\n    Third Line","otherAttrubute":"otherValue"}

I seed my DB in the OnModelCreating of my DbContext:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    var myObjects = new MyObject[]
    {
        [previous objects],
        new MyObject { Id=57, Content "First Line. \nSecond Line.\nThird Line", OtherAttrubute="otherValue" },
        [following objects]
    };

    modelBuilder.Entity<MyObject>().HasData(myObjects);
    base.OnModelCreating(modelBuilder);
}

In my controller I also tried to use Trim() on the Content property before returning my object with return Ok(myObject) but it didn't work.

My guess is that probably this behaviour is caused by Azure SQL Database, but I'm not sure about this.

If you need to see further code just ask. Thank you

Upvotes: 0

Views: 116

Answers (1)

Jack Jia
Jack Jia

Reputation: 5549

\n = CR (Carriage Return) // Used as a new line character in Unix
\r = LF (Line Feed) // Used as a new line character in Mac OS
\n\r = CR + LF // Used as a new line character in Windows

This problem usually occurs when the OS is inconsistent. It seems that the string was escaped again.

You may find the root cause by remote debugging with VS

Upvotes: 1

Related Questions