ssis - beautify json string

Viewed 53

I'm trying to beautify a json string, I'm getting it by:

 string json = JsonConvert.SerializeObject(Row, Formatting.Indented);

and seems it's working pretty well, but it includes _IsNull fields, how I can avoid that?

"ID": 35208,
"ID_IsNull": false,
"name": "SONIA",
"name_IsNull": false

I would have:

"ID": 35208,
"name": "SONIA"
1 Answers

I think the best way is to implement a contract resolver and auto-ignore all properties that have the _IsNull name suffix:

    private class IgnoredNullIndicationPropertiesJsonContractResolver : DefaultContractResolver
    {
        protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
        {
            var property = base.CreateProperty(member, memberSerialization);

            if (property.PropertyName.EndsWith("_IsNull"))
            {
                property.Ignored = true;
            }

            return property;
        }
    }

You can then apply it using a JSON serializer like this:

var serializerSettings = new JsonSerializerSettings
                    {
                        ContractResolver = new IgnoredNullIndicationPropertiesJsonContractResolver(),
                    };

                    string json = JsonConvert.SerializeObject(Row, Formatting.Indented, serializerSettings);
Related