Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to create the below json structure and wanted to keep appending values only under policy defination , but how do i store and add new values along with existing values ? can someone please provide sample c# or idea on how to do it ?







How the output should look like

{
  "properties": {
    "displayName": "Cost Management",
    "description": "Policies to enforce low cost storage SKUs",
    "metadata": {
      "category": "Cost Management"
    },
    "parameters": {
      "namePrefix": {
        "type": "String",
        "defaultValue": "myPrefix",
        "metadata": {
          "displayName": "Prefix to enforce on resource names"
        }
      }
    },   // Wanted to add values under policy defination, the complete json //
    "policyDefinitions": [
      {
        "policyDefinitionId": "/subscriptions/ae640e6b-ba3e-4256-9d62-2993eecfa6f2/providers/Microsoft.Authorization/policyDefinitions/7433c107-6db4-4ad1-b57a-a76dce0154a1",
        "policyDefinitionReferenceId": "Limit_Skus",
        "parameters": {
          "listOfAllowedSKUs": {
            "value": [
              "Standard_GRS",
              "Standard_LRS"
            ]
          }
        }
      },
      {
        "policyDefinitionId": "/subscriptions/ae640e6b-ba3e-4256-9d62-2993eecfa6f2/providers/Microsoft.Authorization/policyDefinitions/ResourceNaming",
        "policyDefinitionReferenceId": "Resource_Naming",
        "parameters": {
          "prefix": {
            "value": "[parameters('namePrefix')]"
          },
          "suffix": {
            "value": "-LC"
          }
        }
      }
    ]
  }
}


What I have tried:

below is the sample code am trying with sample schema 

<pre>if (!blockBlob.Exists())
                        {
                            JObject js = new JObject(
                                new JProperty("properties",
                                new JObject(
                                    new JProperty("displayName", (object)jobject["properties"]["displayName"]),
                                    new JProperty("description", (object)jobject["properties"]["description"]),
                                    new JProperty("policyDefinations",
                                    new JArray(new JObject(
                                        new JProperty("assignedBy", "jaya")))) //,
                                     /*   new JProperty("policydefID", "id")*/)));
                            modifiedfile = js.ToString();
                            await blockBlob.UploadTextAsync(modifiedfile);
                            Console.WriteLine(file);
                        }
                        else
                        {

                            var ms = new MemoryStream();
                            var policyData = blockBlob.DownloadText();

                            var currentJObject = JObject.Parse(policyData);
                            var currentArray = (JArray)currentJObject["properties"]["policyDefinations"];

                            var data = JsonConvert.DeserializeObject<PolicyJsonEntity>(policyData);

                            currentArray.AddAfterSelf(new JObject(
                                   new JArray(new JObject(
                                       new JProperty("assignedBy", "jaya")))));
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900