1- namespace Microsoft . Graph . PowerShell . JsonUtilities
1+ namespace NamespacePrefixPlaceholder . PowerShell . JsonUtilities
22{
33 using Newtonsoft . Json . Linq ;
4+ using System ;
45 using System . Linq ;
56
67 public static class JsonExtensions
78 {
89 /// <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.
1014 /// </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>
1517 /// <example>
1618 /// JObject json = JObject.Parse(@"{""name"": ""John"", ""email"": ""defaultnull"", ""address"": ""null""}");
1719 /// string cleanedJson = json.RemoveDefaultNullProperties();
1820 /// Console.WriteLine(cleanedJson);
1921 /// // Output: { "name": "John", "address": null }
2022 /// </example>
21- public static string RemoveDefaultNullProperties ( this JObject jsonObject )
23+ public static string RemoveDefaultNullProperties ( this JToken token )
2224 {
2325 try
2426 {
25- foreach ( var property in jsonObject . Properties ( ) . ToList ( ) )
27+ if ( token is JObject jsonObject )
2628 {
27- if ( property . Value . Type == JTokenType . Object )
29+ foreach ( var property in jsonObject . Properties ( ) . ToList ( ) )
2830 {
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 )
3432 {
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 ( ) ;
3946 }
4047 }
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 -- )
4653 {
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+ }
4868 }
4969 }
5070 }
51- catch ( System . Exception )
71+ catch ( System . Exception ex )
5272 {
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
5475 }
55- return jsonObject . ToString ( ) ;
76+
77+ return token . ToString ( ) ;
5678 }
79+
5780 public static string ReplaceAndRemoveSlashes ( this string body )
5881 {
5982 return body . Replace ( "/" , "" ) . Replace ( "\\ " , "" ) . Replace ( "rn" , "" ) . Replace ( "\" {" , "{" ) . Replace ( "}\" " , "}" ) ;
6083 }
6184 }
62- }
85+ }
0 commit comments