using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
static JmesPath jmes = new JmesPath();
public static void Test()
string policyStr = GetJson();
string str = jmes.Transform(policyStr, "properties.policyRule.if");
Console.WriteLine("Input JSON:");
Console.WriteLine(JToken.Parse(str));
Console.WriteLine("\nJSON dump:");
""displayName"": ""Audit if Key Vault has no virtual network rules"",
""policyType"": ""Custom"",
""description"": ""Audits Key Vault vaults if they do not have virtual network service endpoints set up. More information on virtual network service endpoints in Key Vault is available here: _https://docs.microsoft.com/en-us/azure/key-vault/key-vault-overview-vnet-service-endpoints"",
""category"": ""Key Vault"",
""equals"": ""Microsoft.KeyVault/vaults""
""field"": ""Microsoft.KeyVault/vaults/networkAcls.virtualNetworkRules[*].id"",
""field"": ""Microsoft.KeyVault/vaults/networkAcls.virtualNetworkRules[*].id"",
""field"": ""Microsoft.KeyVault/vaults/networkAcls.defaultAction"",
""id"": ""/subscriptions/xxxxxx/providers/Microsoft.Authorization/policyDefinitions/wkpolicydef"",
""type"": ""Microsoft.Authorization/policyDefinitions"",
public static void Convert(string json)
dynamic myObj = JsonConvert.DeserializeObject(json);
private static void PrintObject(JToken token, int depth)
var jProp = (JProperty)token;
var spacer = string.Join("", Enumerable.Range(0, depth).Select(_ => "\t"));
var val = jProp.Value is JValue ? ((JValue)jProp.Value).Value : "-";
Console.WriteLine(string.Format("{0}{1} -> {2}", spacer, jProp.Name, val));
foreach (var child in jProp.Children())
PrintObject(child, depth + 1);
else if (token is JObject)
foreach (var child in ((JObject)token).Children())
PrintObject(child, depth + 1);
public static void Main()
Console.WriteLine("Environment version: " + Environment.Version);
Console.WriteLine("Json.NET version: " + typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");