0

I have nested JSON in PHP, I am new to PHP and I am unable to read the below-nested JSON. Here is the long JSON file.

<?php
$nestedjson='{
    "value": [
      {
        "name": "POOL1",
        "id": "/subscriptions/2xxxx-xxxx-xxxx-xxxx/resourcegroups/XXXX/providers/Microsoft.DesktopVirtualization/hostpools/AZREUSLSHP1",
        "type": "Microsoft.DesktopVirtualization/hostpools",
        "location": "eastus",
        "tags": {
          "owner": "Domain",
          "department": "TPW",
          "workLoadType": "WVD",
          "contactName": "testuser1",
          "CostBucket": "bucket1"
        },
        "kind": null,
        "properties": {
          "friendlyName": null,
          "description": "Created through the WVD extension",
          "hostPoolType": "Pooled",
          "personalDesktopAssignmentType": null,
          "applicationGroupReferences": [
            "/subscriptions/xxxx-xxxx-xxxx-xxxx/resourcegroups/XXXX/providers/Microsoft.DesktopVirtualization/applicationgroups/AZREUSLSHP1-DAG",
            "/subscriptions/xxxx-xxxx-xxxx-xxxx/resourcegroups/XXXX/providers/Microsoft.DesktopVirtualization/applicationgroups/AZREUSFINGRP"
          ],
          "customRdpProperty": "",
          "maxSessionLimit": 6,
          "loadBalancerType": "BreadthFirst",
          "validationEnvironment": false,
          "ring": null,
          "registrationInfo": {
            "expirationTime": null,
            "token": null,
            "resetToken": false,
            "registrationTokenOperation": "None"
          },
          "vmTemplate": "{\"domain\":\"XXXX\",\"galleryImageOffer\":null,\"galleryImagePublisher\":null,\"galleryImageSKU\":null,\"imageType\":\"CustomImage\",\"imageUri\":null,\"customImageId\":\"/subscriptions/XXXX/resourceGroups/IMAGEGALLERYRG/providers/Microsoft.Compute/galleries/WVDImageGallery3/images/WVDBaseImageDefinition1\",\"namePrefix\":\"AZREUSWVD\",\"osDiskType\":\"StandardSSD_LRS\",\"useManagedDisks\":true,\"vmSize\":{\"id\":\"Standard_B2ms\",\"cores\":2,\"ram\":8},\"galleryItemId\":null}",
          "preferredAppGroupType": "Desktop",
          "migrationRequest": null,
          "cloudPcResource": false,
          "startVMOnConnect": false,
          "ssoadfsAuthority": null,
          "ssoClientId": null,
          "ssoClientSecretKeyVaultPath": null,
          "ssoSecretType": null,
          "objectId": "3a5db190-342d-441e-9798-667079784cbf"
        }
      },
      {
        "name": "POOL2",
        "id": "/subscriptions/xxxx-xxxx-xxxx-xxxx/resourcegroups/XXXX/providers/Microsoft.DesktopVirtualization/hostpools/AZREUSLSHP2",
        "type": "Microsoft.DesktopVirtualization/hostpools",
        "location": "eastus",
        "tags": {
         "owner": "Domain",
          "department": "TPW",
          "workLoadType": "WVD",
          "contactName": "testuser2",
          "CostBucket": "bucket2"
        },
        "kind": null,
        "properties": {
          "friendlyName": null,
          "description": "Created through the WVD extension",
          "hostPoolType": "Personal",
          "personalDesktopAssignmentType": "Direct",
          "applicationGroupReferences": [
            "/subscriptions/xxxx-xxxx-xxxx-xxxx/resourcegroups/XXXX/providers/Microsoft.DesktopVirtualization/applicationgroups/AZREUSLSHP2-DAG"
          ],
          "customRdpProperty": "",
          "maxSessionLimit": 999999,
          "loadBalancerType": "Persistent",
          "validationEnvironment": false,
          "ring": null,
          "registrationInfo": {
            "expirationTime": null,
            "token": null,
            "resetToken": false,
            "registrationTokenOperation": "None"
          },
          "vmTemplate": "{\"domain\":\"domain.com\",\"galleryImageOffer\":\"Windows-10\",\"galleryImagePublisher\":\"microsoftwindowsdesktop\",\"galleryImageSKU\":\"19h2-ent-g2\",\"imageType\":\"Gallery\",\"imageUri\":null,\"customImageId\":null,\"namePrefix\":\"AZREUSWVDP\",\"osDiskType\":\"StandardSSD_LRS\",\"useManagedDisks\":true,\"vmSize\":{\"id\":\"Standard_B2s\",\"cores\":2,\"ram\":4},\"galleryItemId\":\"microsoftwindowsdesktop.windows-1019h2-ent-g2\"}",
          "preferredAppGroupType": "Desktop",
          "migrationRequest": null,
          "cloudPcResource": false,
          "startVMOnConnect": false,
          "ssoadfsAuthority": null,
          "ssoClientId": null,
          "ssoClientSecretKeyVaultPath": null,
          "ssoSecretType": null,
          "objectId": "d187b18a-baa7-4e59-97ad-f84a1f50186e"
        }
      },
      {
        "name": "POOL3",
        "id": "/subscriptions/xxxx-xxxx-xxxx-xxxx/resourcegroups/AZREUSLSWVDREFRG/providers/Microsoft.DesktopVirtualization/hostpools/ReferenceHostPool",
        "type": "Microsoft.DesktopVirtualization/hostpools",
        "location": "eastus",
        "tags": null,
        "kind": null,
        "properties": {
          "friendlyName": null,
          "description": null,
          "hostPoolType": "Pooled",
          "personalDesktopAssignmentType": null,
          "applicationGroupReferences": [
            "/subscriptions/xxxx-xxxx-xxxx-xxxx/resourcegroups/AZREUSLSWVDREFRG/providers/Microsoft.DesktopVirtualization/applicationgroups/RefAppGroup"
          ],
          "customRdpProperty": "",
          "maxSessionLimit": 5,
          "loadBalancerType": "DepthFirst",
          "validationEnvironment": false,
          "ring": null,
          "registrationInfo": {
            "expirationTime": null,
            "token": null,
            "resetToken": false,
            "registrationTokenOperation": "None"
          },
          "vmTemplate": null,
          "preferredAppGroupType": "Desktop",
          "migrationRequest": null,
          "cloudPcResource": false,
          "startVMOnConnect": false,
          "ssoadfsAuthority": null,
          "ssoClientId": null,
          "ssoClientSecretKeyVaultPath": null,
          "ssoSecretType": null,
          "objectId": "1f06e1c3-669e-4227-bb7c-386b634c6c30"
        }
      }
    ],
    "nextLink": null
  }'

I would like to read and print the values of "hostPoolType" from the above JSON, I have tried the below code it didn't work.

$arr = json_decode($nestedjson, true);

$hostpooltype = $arr['properties']['hostPoolType'];

//print_r($hostpooltype);

foreach($hostpooltype as $item)
{
    echo $item;
    echo '<br>';
}
?>

It's not returning the correct result. Can anyone please help here?

3
  • 2
    Shouldn't it be $arr['value'][0]['properties']['hostPoolType']? Commented Feb 21, 2021 at 17:21
  • Yes, you are correct, however, I would like to read the values of the array[1] and [2] as well. I think I need to find the array length and need to run a for loop? Commented Feb 21, 2021 at 17:27
  • So then if you need values from 1-2 as well you need to loop through them. You just can't ignore the one key and expect to it iterate through for you... Commented Feb 21, 2021 at 19:30

3 Answers 3

4

you should run loop for each value key of json array. use this.

<?php
$arr = json_decode($nestedjson, true);
foreach ($arr['value'] as $newArr){
    echo $newArr['properties']['hostPoolType'];
    echo '<br>';
}
?>
Sign up to request clarification or add additional context in comments.

Comments

0

I have found the solution here.

$arr = json_decode($nestedjson, true);
$hostpooltype = array();
$names = array();
foreach ($arr['value'] as $newArr)
{

  $hostpooltype[] = $newArr['properties']['hostPoolType'];
  $names[] = $newArr ['name'];
}
// Print the values of the A
foreach($hostpooltype as $value){
  echo $value . "<br>";
}

Comments

0

Try to use array_walk function:

$arr = json_decode($nestedjson);

function recursiveWalk($value, $key)
{
    if (is_array($value)) {
        array_walk($value, "recursiveWalk");
    }
    if (is_object($value)) {
        echo "{$value->properties->hostPoolType}<br>";
    }
}
array_walk($arr, "recursiveWalk");

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.