Govind
Govind

Reputation: 979

Remove/Replace newline character from JSON in .Net using JsonConvert

I need to remove some newline characters from some string values while serializing to JSON -- i.e. during c# object to JSON conversion. How can this be done?

public static string ToJson(this object value)
{
  //Serializing C# Object to Json which gives me below mentioned JSON.
  return JsonConvert.SerializeObject(value);
}

Below is my sample JSON:

{
    "PolicyHolderInfo": {
        "ActionDate": "20190912",
        "EmployeeCode": "EPL2",
        "LocalEntityCode": "K",
        "PolicyHolderCode": "1YAAAAC",
        "Name1": "monoliability PDL\n",
        "Name2": "test\n",
        "Street1": "-",
        "Street2": "-",
        "City": "-",
        "PostCode": "-",
        "Subdivision": "01",
        "FMEPaymentFrequency1": "00",       
        "Reference": "C00-AAC"
    },
    "PolicyHolderTrailerInfo": {
        "RecordCode": "Z",
        "CountryCode": "KSA",
        "SourceIndicator": "EPLC",
        "RecordSequence": 0,
        "LinkNumber": 0,
        "RecordType": "Z",
        "ReturnCode": null,
        "Reference": "1-AC"
    },
    "SourceSystemDate": "2019-09-17T17:48:31.3543314+08:00"
}

The Name1 and Name2 has newline character \n. I tried many options to remove those \n. Instead of looping through every value, I am just trying to replace globally if any new line characters present. None of the below options are working?

C#:

var str1 = JsonConvert.ToString(value);
var str2 = value.Replace(System.Environment.NewLine, string.Empty);
Regex regex_newline = new Regex("(\r\n|\r|\n)");
var str3 = regex_newline.Replace(value, "");
var str4 = value.Replace("\n", string.Empty);  

The 'value' in above code is json mentioned in the sample.

Upvotes: 3

Views: 23485

Answers (1)

dbc
dbc

Reputation: 116585

You can modify the logic of ReplacingStringConverter from this answer to running a transformation on a Json DeserializeObject for a property to remove \n characters while serializing string value(s).

First, define the following converter:

public class ReplacingStringWritingConverter : JsonConverter
{
    readonly string oldValue;
    readonly string newValue;

    public ReplacingStringWritingConverter(string oldValue, string newValue)
    {
        if (string.IsNullOrEmpty(oldValue))
            throw new ArgumentException("string.IsNullOrEmpty(oldValue)");
        if (newValue == null)
            throw new ArgumentNullException("newValue");
        this.oldValue = oldValue;
        this.newValue = newValue;
    }

    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(string);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }

    public override bool CanRead { get { return false; } }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        var s = ((string)value).Replace(oldValue, newValue);
        writer.WriteValue(s);
    }
}

Then you can serialize your root data model as follows:

var settings = new JsonSerializerSettings { Converters = { new ReplacingStringWritingConverter("\n", "") } };
var newJson = JsonConvert.SerializeObject(root, Formatting.None, settings); 

The converter works when serializing both typed data models with a fixed schema and LINQ to JSON freeform JSON captured in a JToken hierarchy.

(As an aside, I believe your value.Replace() is failing because JSON string values never actually contain newline characters. Instead, as specified by the JSON standard, a newline character must be replaced by the two-character escape sequence \n. Thus value.Replace("\n", string.Empty) fails, as the \n in your code is the c# escape sequence for an actual newline character -- which is not present.)

Demo fiddle here.

Upvotes: 4

Related Questions