Search code examples
phpjsonassociative-array

How I can read and update the nested JSON in PHP


I am new to PHP and have little experience with PHP arrays, I have this below JSON file.

$json2=
'{
  "location": "westus",
  "properties": {
    "hardwareProfile": {
      "vmSize": "Standard_D1_v2"
    },
    "storageProfile": {
      "imageReference": {
        "sku": "2016-Datacenter",
        "publisher": "MicrosoftWindowsServer",
        "version": "latest",
        "offer": "WindowsServer"
      },
      "osDisk": {
        "caching": "ReadWrite",
        "managedDisk": {
          "storageAccountType": "Standard_LRS"
        },
        "name": "myVMosdisk",
        "createOption": "FromImage"
      }
    },
    "networkProfile": {
      "networkInterfaces": [
        {
          "id": "/subscriptions/{subscription-id}/resourceGroups/myResourceGroup/providers/Microsoft.Network/networkInterfaces/{existing-nic-name}",
          "properties": {
            "primary": true
          }
        }
      ]
    },
    "osProfile": {
      "adminUsername": "{your-username}",
      "computerName": "myVM",
      "adminPassword": "{your-password}"
    },
    "diagnosticsProfile": {
      "bootDiagnostics": {
        "storageUri": "http://{existing-storage-account-name}.blob.core.windows.net",
        "enabled": true
      }
    }
  }
}';

I want to update the values for 'location', 'vmSize','sku','publisher','offer' in the above JSON,

"location" : "eastus"
"vmSize" : "Standard_D3_v2"
"sku" : "20h1-evd"
"publisher" : "MicrosoftWindowsDesktop"
"offer" : "windows-10" 

I have tried to do the following to this but I am nowhere near navigating the array correctly.

$arr = json_decode($json2, true);
print_r($arr);

Can anyone please help here?


Solution

  • Just try this code

    <?php
    
    $json2=
    '{
      "location": "westus",
      "properties": {
        "hardwareProfile": {
          "vmSize": "Standard_D1_v2"
        },
        "storageProfile": {
          "imageReference": {
            "sku": "2016-Datacenter",
            "publisher": "MicrosoftWindowsServer",
            "version": "latest",
            "offer": "WindowsServer"
          },
          "osDisk": {
            "caching": "ReadWrite",
            "managedDisk": {
              "storageAccountType": "Standard_LRS"
            },
            "name": "myVMosdisk",
            "createOption": "FromImage"
          }
        },
        "networkProfile": {
          "networkInterfaces": [
            {
              "id": "/subscriptions/{subscription-id}/resourceGroups/myResourceGroup/providers/Microsoft.Network/networkInterfaces/{existing-nic-name}",
              "properties": {
                "primary": true
              }
            }
          ]
        },
        "osProfile": {
          "adminUsername": "{your-username}",
          "computerName": "myVM",
          "adminPassword": "{your-password}"
        },
        "diagnosticsProfile": {
          "bootDiagnostics": {
            "storageUri": "http://{existing-storage-account-name}.blob.core.windows.net",
            "enabled": true
          }
        }
      }
    }';
    
    $arr = json_decode($json2, true);
    $arr['location'] = "eastus";
    $arr['properties']['hardwareProfile']['vmSize'] = "Standard_D3_v2";
    $arr['properties']['storageProfile']['imageReference']['sku'] = "20h1-evd";
    $arr['properties']['storageProfile']['imageReference']['publisher'] = "MicrosoftWindowsDesktop";
    $arr['properties']['storageProfile']['imageReference']['offer'] = "WindowsServer";
    $updatedJson = json_encode($arr);
    
    print_r($updatedJson);
    

    First you need to try json_decode to make an array from this json file and then based on the array schema you need to find the property that you want and update it and then again use the json_encode to make an json