0

I'm trying to access some nested values in my JSON body that I retrieve.

The request url is as follows: https://pilotapi01.telkom.co.za/gateway/serviceQualification/v1/46165547

This returns the following JSON:

{
  "id": "46165547",
  "href": "/serviceQualificationRequest/46165547",
  "interactionDate": "20220704 08:49:02",
  "description": "OpenServe Service Qualification",
  "eligibilityDate": "20220704 08:49:02",
  "qualificationState": "Done",
  "address": {
    "id": "46165547",
    "href": "/api/address/46165547",
    "customerPointRegion": "None",
    "localExchangeName": "None",
    "localStationCode": "None",
    "MEPOPRegion": "None",
    "MEExchangeName": "None",
    "DistanceCustomerToMetroMDF": "-1",
    "DistanceCustomerToLocalMDF": "-1",
    "DistanceLocalMDFtoMEMDF": "-1"
  },
  "addressDescription": {
    "mduFlag": "NO",
    "streetNr": "92",
    "streetName": "MILLS",
    "streetType": "ST",
    "locality": "STRAND",
    "city": "STRAND",
    "stateOrProvince": "WESTERN CAPE",
    "country": "South Africa"
  },
  "geoCode": {
    "latitude": "-34.108223",
    "longitude": "18.835977",
    "geographicDatum": "WGS84"
  },
  "physicalTerminationPoint": [
    {
      "accessType": "SPACE"
    },
    {
      "accessType": "DSL"
    },
    {
      "accessType": "FIBRE"
    }
  ],
  "serviceQualificationItem": [
    {
      "service": {
        "serviceCharacteristic": [
          {
            "name": "Description",
            "value": "SPACE"
          }
        ],
        "serviceSpecification": {
          "id": "OPENSERVE SPACE CONNECT",
          "href": "https://connect.openserve.co.za/product/broadband/space-connect",
          "serviceCategoryId": "SPACE",
          "serviceSpecificationCharacteristic": [
            {
              "id": "GIS001",
              "name": "upstreamSpeed",
              "valueto": "50 Mbps"
            },
            {
              "id": "GIS002",
              "name": "downstreamSpeed",
              "valueto": "50 Mbps"
            }
          ]
        }
      },
      "availability": "Available",
      "serviceabilityDate": "20220704 08:49:02"
    },
    {
      "service": {
        "serviceCharacteristic": [
          {
            "name": "Description",
            "value": "DSL"
          }
        ],
        "serviceSpecification": {
          "id": "OPENSERVE COPPER CONNECT",
          "href": "https://connect.openserve.co.za/product/broadband/copper-connect",
          "serviceCategoryId": "DSL",
          "serviceSpecificationCharacteristic": [
            {
              "id": "GIS001",
              "name": "upstreamSpeed",
              "valueto": "0.5 Mbps"
            },
            {
              "id": "GIS002",
              "name": "downstreamSpeed",
              "valueto": "20 Mbps"
            }
          ]
        }
      },
      "availability": "available",
      "serviceabilityDate": "20220704 08:49:02"
    },
    {
      "service": {
        "serviceCharacteristic": [
          {
            "name": "Description",
            "value": "DSL"
          }
        ],
        "serviceSpecification": {
          "id": "OPENSERVE PURE CONNECT",
          "href": "https://connect.openserve.co.za/product/broadband/pure-connect",
          "serviceCategoryId": "DSL",
          "serviceSpecificationCharacteristic": [
            {
              "id": "GIS001",
              "name": "upstreamSpeed",
              "valueto": "0.5 Mbps"
            },
            {
              "id": "GIS002",
              "name": "downstreamSpeed",
              "valueto": "20 Mbps"
            }
          ]
        }
      },
      "availability": "available",
      "serviceabilityDate": "20220704 08:49:02"
    },
    {
      "service": {
        "serviceCharacteristic": [
          {
            "name": "Description",
            "value": "FIBRE"
          }
        ],
        "serviceSpecification": {
          "id": "OPENSERVE FIBRE CONNECT",
          "href": "https://connect.openserve.co.za/product/broadband/fibre-connect",
          "serviceCategoryId": "FIBRE",
          "serviceSpecificationCharacteristic": [
            {
              "id": "GIS001",
              "name": "upstreamSpeed",
              "valueto": "250 Mbps"
            },
            {
              "id": "GIS002",
              "name": "downstreamSpeed",
              "valueto": "500 Mbps"
            }
          ]
        }
      },
      "availability": "Unavailable"
    },
    {
      "service": {
        "serviceCharacteristic": [
          {
            "name": "Description",
            "value": "FIBRE"
          }
        ],
        "serviceSpecification": {
          "id": "OPENSERVE OFFICE CONNECT FIBRE",
          "href": "https://connect.openserve.co.za/product/broadband/fibre-connect",
          "serviceCategoryId": "FIBRE",
          "serviceSpecificationCharacteristic": [
            {
              "id": "GIS001",
              "name": "upstreamSpeed",
              "valueto": "250 Mbps"
            },
            {
              "id": "GIS002",
              "name": "downstreamSpeed",
              "valueto": "500 Mbps"
            }
          ]
        }
      },
      "availability": "Unavailable"
    },
    {
      "service": {
        "serviceCharacteristic": [
          {
            "name": "Description",
            "value": "FIBRE"
          }
        ],
        "serviceSpecification": {
          "id": "OPENSERVE INFRASTRUCTURE BROADBAND",
          "href": "https://connect.openserve.co.za/product/broadband/fibre-connect",
          "serviceCategoryId": "FIBRE",
          "serviceSpecificationCharacteristic": [
            {
              "id": "GIS001",
              "name": "upstreamSpeed",
              "valueto": "250 Mbps"
            },
            {
              "id": "GIS002",
              "name": "downstreamSpeed",
              "valueto": "500 Mbps"
            }
          ]
        }
      },
      "availability": "Unavailable"
    }
  ]
}

I want to access serviceQualificationItem->serviceSpecification->serviceSpecificationCharacteristic's values so I can use them in a foreach loop to output like this example:

// From previous request
$addressID = $address->id;

    $request2 = wp_remote_get( 'https://pilotapi01.telkom.co.za/gateway/serviceQualification/v1/'.$addressID.'');
    $info = wp_remote_retrieve_body( $request2 );

    $package = json_decode( $info , true );


    if( ! empty( $package ) ) {
        echo '<div class="output-wrapper">';
        foreach( $package as $details ) {?>
            <div class="package-box">
                <div><b>ID: </b> <?php echo $details->serviceQualificationItem->serviceSpecification->serviceSpecificationCharacteristic->ID ?> </div>                
            </div>
        <?php }
        echo '</div>';
    }
Demonix
  • 43
  • 9
  • ``$package`` is a single object not an array, you shouldn't be loop-ing it, instead you should loop through ``$package['serviceQualificationItem'] as $details`` and then you need another loop for ``$details['service']['serviceSpecification']['serviceSpecificationCharacteristic']`` since it's an array of objects – OMi Shah Jul 04 '22 at 07:46

1 Answers1

1

The code required in order to loop over what you ask is this:

$request2 = wp_remote_get( 'https://pilotapi01.telkom.co.za/gateway/serviceQualification/v1/46165547' );
$info     = wp_remote_retrieve_body( $request2 );

$package = json_decode( $info, true );
if ( ! empty( $package ) ) {
    foreach ( $package["serviceQualificationItem"] as $serverQualificationItem ) {
        if ( $serverQualificationItem["availability"] == "Available" ) { // Maybe you want to get only the available ones? because i noticed some are unavailable
            foreach ( $serverQualificationItem["service"]["serviceSpecification"]["serviceSpecificationCharacteristic"] as $serviceSpecificationCharacteristic ) {
                ?>
                <div class="package-box">
                    <div><b>ID: </b> <?php echo $serviceSpecificationCharacteristic->ID ?> </div>
                </div>
                <?php
            }
        }
    }
}

Tested and works since the link you provided is public (be careful posting that here if it contains sensitive data)

I noticed that some services are "unavailable" and added an extra "if" that you may/may not need. This should give you a head start, if you need to modify it just be careful with the nesting keys

Diego
  • 1,610
  • 1
  • 14
  • 26
  • Okay thanks @Diego - that helped me along. I just had to update $serviceSpecificationCharacteristic->ID to $serviceSpecificationCharacteristic['id'] to get it to display. – Demonix Jul 04 '22 at 08:12
  • @berend you've got a point, english is not my primary language =(. Edited tho – Diego Jul 04 '22 at 08:15