How to remove fields from JSON dynamically in dotnet using Json.Net

TL;DR Just parse JSON into a JToken, somehow try to match property names. Wherever there’s a match, you can remove the property from its parent object. RemoveFromLowestPossibleParent Use method RemoveFromLowestPossibleParent from nice JsonExtensions class from Brian Rogers stackoverflow answer Code public static partial class JsonExtensions { public static TJToken RemoveFromLowestPossibleParent<TJToken>(this TJToken node) where TJToken : JToken { if (node == null) return null; JToken toRemove; var property = node.Parent as JProperty; if (property != null) { // Also detach the node from its immediate containing property -- Remove() does not do this even though it seems like it should toRemove = property; property.Value = null; } else { toRemove = node; } if (toRemove.Parent != null) toRemove.Remove(); return node; } public static IEnumerable<TJToken> RemoveFromLowestPossibleParents<TJToken>(this IEnumerable<TJToken> nodes) where TJToken : JToken { var list = nodes.ToList(); foreach (var node in list) node.RemoveFromLowestPossibleParent(); return list; } } Json field name from lambda expression Using PropertyHelper<YourClass>.GetJsonName(c => c.Id) on property with JsonPropertyAttribute attribute will return you json field name for this property. ...

February 17, 2022

Today I Learned: Nuget commands

The following command will update all packages in every project to the latest version available from nuget.org. Update-Package You can also restrict this down to one project. Update-Package -Project YourProjectName If you want to reinstall the packages to the same versions as were previously installed then you can use the -reinstall argument with Update-Package command. Update-Package -reinstall You can also restrict this down to one project. Update-Package -reinstall -Project YourProjectName The -reinstall option will first uninstall and then install the package back again into a project. ...

October 28, 2016

How to parse HTML in .NET

Small post about different parsers in .NET with can help you to work with HTML files. HtmlAgilityPack HtmlAgilityPack is one of the most famous HTML parser in .NET world. This is an agile HTML parser that builds a read/write DOM and supports plain XPATH or XSLT (you actually don’t HAVE to understand XPATH nor XSLT to use it, don’t worry…). It is a .NET code library that allows you to parse “out of the web” HTML files. The parser is very tolerant with “real world” malformed HTML. The object model is very similar to what proposes System.Xml, but for HTML documents (or streams). ...

December 21, 2015

Today I Learned: Ninject in OWIN WebApi application

This chapter explains how to setup an application in case you want to use the OWIN (Open Web Interface for .NET) interface to be able to move your application between OWIN supporting hosts without making code changes. Requirements Version You will need Version 3.2.4 or newer, otherwise you will get a Ninject.ActivationException regarding HttpConfiguration. Installation via nuget First you have to add a reference to Ninject.Web.Common.OwinHost and Ninject.Web.WebApi.OwinHost either by adding the reference manually or installing the corresponding NuGet Package. ...

December 21, 2015

Today I Learned: SharpRepository

I absolutely forgot about the blog. My bad. Let’s add something interesting about C#. SharpRepository SharpRepository is a generic repository written in C# which includes support for various relational, document and object databases including Entity Framework, RavenDB, MongoDB, CouchDB and Db4o. SharpRepository includes Xml and InMemory repository implementations as well. SharpRepository offers built-in caching options for AppFabric, memcached and the standard System.Runtime.Caching. SharpRepository also supports Specifications, FetchStrategies, Batches and Traits. ...

December 17, 2015

Today I Learned: Sanitize File Name in C#

A short update about safe file names in C#. Sanitize File Name in C# To clean up a file name you could do this private static string MakeValidFileName(string name) { string invalidChars = new string(Path.GetInvalidFileNameChars()) string escapedInvalidChars = Regex.Escape(); string invalidRegex = string.Format(@"([{0}]*\.+$)|([{0}]+)", escapedInvalidChars); return Regex.Replace(name, invalidRegex, "_"); } Working example you can find on dotnetfiddle

November 17, 2015