1
- namespace Microsoft . Graph . PowerShell . JsonUtilities
1
+ namespace NamespacePrefixPlaceholder . PowerShell . JsonUtilities
2
2
{
3
3
using Newtonsoft . Json . Linq ;
4
+ using System ;
4
5
using System . Linq ;
5
6
6
7
public static class JsonExtensions
7
8
{
8
9
/// <summary>
9
- /// Removes JSON properties that have a value of "defaultnull" and converts properties with values of "null" to actual JSON null values.
10
+ /// Recursively removes properties with the value "defaultnull" from a JSON structure
11
+ /// and replaces string values that are "null" with actual null values.
12
+ /// This method supports both JObject (JSON objects) and JArray (JSON arrays),
13
+ /// ensuring proper cleanup of nested structures.
10
14
/// </summary>
11
- /// <param name="jsonObject">The JObject to process and clean.</param>
12
- /// <returns>
13
- /// A JSON string representation of the cleaned JObject with "defaultnull" properties removed and "null" values converted to JSON null.
14
- /// </returns>
15
+ /// <param name="token">The JToken (JObject or JArray) to process.</param>
16
+ /// <returns>The cleaned JSON string with "defaultnull" values removed and "null" strings converted to null.</returns>
15
17
/// <example>
16
18
/// JObject json = JObject.Parse(@"{""name"": ""John"", ""email"": ""defaultnull"", ""address"": ""null""}");
17
19
/// string cleanedJson = json.RemoveDefaultNullProperties();
18
20
/// Console.WriteLine(cleanedJson);
19
21
/// // Output: { "name": "John", "address": null }
20
22
/// </example>
21
- public static string RemoveDefaultNullProperties ( this JObject jsonObject )
23
+ public static string RemoveDefaultNullProperties ( this JToken token )
22
24
{
23
25
try
24
26
{
25
- foreach ( var property in jsonObject . Properties ( ) . ToList ( ) )
27
+ if ( token is JObject jsonObject )
26
28
{
27
- if ( property . Value . Type == JTokenType . Object )
29
+ foreach ( var property in jsonObject . Properties ( ) . ToList ( ) )
28
30
{
29
- RemoveDefaultNullProperties ( ( JObject ) property . Value ) ;
30
- }
31
- else if ( property . Value . Type == JTokenType . Array )
32
- {
33
- foreach ( var item in property . Value )
31
+ if ( property . Value . Type == JTokenType . Object )
34
32
{
35
- if ( item . Type == JTokenType . Object )
36
- {
37
- RemoveDefaultNullProperties ( ( JObject ) item ) ;
38
- }
33
+ RemoveDefaultNullProperties ( property . Value ) ;
34
+ }
35
+ else if ( property . Value . Type == JTokenType . Array )
36
+ {
37
+ RemoveDefaultNullProperties ( property . Value ) ;
38
+ }
39
+ else if ( property . Value . Type == JTokenType . String && property . Value . ToString ( ) . Equals ( "defaultnull" , StringComparison . Ordinal ) )
40
+ {
41
+ property . Remove ( ) ;
42
+ }
43
+ else if ( property . Value . Type == JTokenType . String && property . Value . ToString ( ) . Equals ( "null" , StringComparison . Ordinal ) )
44
+ {
45
+ property . Value = JValue . CreateNull ( ) ;
39
46
}
40
47
}
41
- else if ( property . Value . Type == JTokenType . String && property . Value . ToString ( ) == "defaultnull" )
42
- {
43
- property . Remove ( ) ;
44
- }
45
- else if ( property . Value . Type == JTokenType . String && ( property . Value . ToString ( ) == "null" ) )
48
+ }
49
+ else if ( token is JArray jsonArray )
50
+ {
51
+ // Process each item in the JArray
52
+ for ( int i = jsonArray . Count - 1 ; i >= 0 ; i -- )
46
53
{
47
- property . Value = JValue . CreateNull ( ) ;
54
+ var item = jsonArray [ i ] ;
55
+
56
+ if ( item . Type == JTokenType . Object )
57
+ {
58
+ RemoveDefaultNullProperties ( item ) ;
59
+ }
60
+ else if ( item . Type == JTokenType . String && item . ToString ( ) . Equals ( "defaultnull" , StringComparison . Ordinal ) )
61
+ {
62
+ jsonArray . RemoveAt ( i ) ; // Remove the "defaultnull" string from the array
63
+ }
64
+ else if ( item . Type == JTokenType . String && item . ToString ( ) . Equals ( "null" , StringComparison . Ordinal ) )
65
+ {
66
+ jsonArray [ i ] = JValue . CreateNull ( ) ; // Convert "null" string to actual null
67
+ }
48
68
}
49
69
}
50
70
}
51
- catch ( System . Exception )
71
+ catch ( System . Exception ex )
52
72
{
53
- return jsonObject . ToString ( ) ; // Return the original string if parsing fails
73
+ Console . WriteLine ( $ "Error cleaning JSON: { ex . Message } ") ;
74
+ return token . ToString ( ) ; // Return the original JSON if any error occurs
54
75
}
55
- return jsonObject . ToString ( ) ;
76
+
77
+ return token . ToString ( ) ;
56
78
}
79
+
57
80
public static string ReplaceAndRemoveSlashes ( this string body )
58
81
{
59
82
return body . Replace ( "/" , "" ) . Replace ( "\\ " , "" ) . Replace ( "rn" , "" ) . Replace ( "\" {" , "{" ) . Replace ( "}\" " , "}" ) ;
60
83
}
61
84
}
62
- }
85
+ }
0 commit comments