48 lines
1.4 KiB
C#
Executable File
48 lines
1.4 KiB
C#
Executable File
using Newtonsoft.Json.Linq;
|
|
|
|
namespace audit_logs.Utils;
|
|
|
|
public static class JsonClean
|
|
{
|
|
public static JObject CleanJObject(JObject obj)
|
|
{
|
|
var properties = obj.Properties().ToList();
|
|
foreach (var property in properties)
|
|
{
|
|
if (property.Value.Type == JTokenType.Null)
|
|
{
|
|
// Eliminar la propiedad si el valor es null
|
|
property.Remove();
|
|
}
|
|
else if (property.Value.Type == JTokenType.Object)
|
|
{
|
|
// Limpieza para objetos anidados
|
|
property.Value = CleanJObject((JObject)property.Value);
|
|
}
|
|
else if (property.Value.Type == JTokenType.Array)
|
|
{
|
|
// Limpieza para arrays, aplicando limpieza a cada objeto dentro del array
|
|
property.Value = CleanJArray((JArray)property.Value);
|
|
}
|
|
}
|
|
return obj;
|
|
}
|
|
|
|
public static JArray CleanJArray(JArray array)
|
|
{
|
|
var cleanedArray = new JArray();
|
|
foreach (var item in array)
|
|
{
|
|
if (item.Type == JTokenType.Object)
|
|
{
|
|
cleanedArray.Add(CleanJObject((JObject)item));
|
|
}
|
|
else if (item.Type != JTokenType.Null)
|
|
{
|
|
cleanedArray.Add(item);
|
|
}
|
|
}
|
|
return cleanedArray;
|
|
}
|
|
|
|
} |