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: Azure Cosmos Db: Query for Fields not defined

There are several ways: Using LINQ For anyone who wants a Linq solution, it’s: query.Where(d => !d.SomeField.IsDefined()) Note: To get the IsDefined extension method, you need to add Microsoft.Azure.Documents.SystemFunctions namespace Using SQL Query Grammar with built-in functions IS_DEFINED (expr) - returns a Boolean indicating if the property has been assigned a value. SELECT TOP {0} * FROM Telemetry t WHERE (is_defined(t.longitude) OR is_defined(t.latitude)) Thanks to Azure Feedback and Azure Cosmos DB SQL syntax reference

December 27, 2017

Today I Learned: Undo checkout of unmodified files in TFS

The following command will undo checkout of unmodified files in TFS c:\myProject> tfpt uu . /noget /recursive Thanks to stackoverflow.

March 1, 2017