0

Building an associative array from json object is necessary when making an API call to Walmart. Could anyone please help me get the associative array from this object?

{
  "orderShipment": {
    "orderLines": {
      "orderLine": [
        {
          "lineNumber": "1",
          "sellerOrderId": "92344",
          "orderLineStatuses": {
            "orderLineStatus": [
              {
                "status": "Shipped",
                "statusQuantity": {
                  "unitOfMeasurement": "EACH",
                  "amount": "1"
                },
                "trackingInfo": {
                  "shipDateTime": 1580821866000,
                  "carrierName": {
                    "otherCarrier": null,
                    "carrier": "UPS"
                  },
                  "methodCode": "Standard",
                  "trackingNumber": "22344",
                  "trackingURL": "http://walmart/tracking/ups?&type=MP&seller_id=12345&promise_date=03/02/2020&dzip=92840&tracking_numbers=92345"
                },
                "returnCenterAddress": {
                  "name": "walmart",
                  "address1": "walmart store 2",
                  "address2": null,
                  "city": "Huntsville",
                  "state": "AL",
                  "postalCode": "35805",
                  "country": "USA",
                  "dayPhone": "12344",
                  "emailId": "[email protected]"
                }
              }
            ]
          }
        },
        {
          "lineNumber": "2",
          "sellerOrderId": "92344",
          "orderLineStatuses": {
            "orderLineStatus": [
              {
                "status": "Shipped",
                "statusQuantity": {
                  "unitOfMeasurement": "EACH",
                  "amount": "1"
                },
                "trackingInfo": {
                  "shipDateTime": 1580821866000,
                  "carrierName": {
                    "otherCarrier": null,
                    "carrier": "FedEx"
                  },
                  "methodCode": "Express",
                  "trackingNumber": "22344",
                  "trackingURL": "http://walmart/tracking/fedEx?&type=MP&seller_id=12345&promise_date=03/02/2020&dzip=92840&tracking_numbers=92344"
                },
                "returnCenterAddress": {
                  "name": "walmart",
                  "address1": "walmart store 2",
                  "address2": null,
                  "city": "Huntsville",
                  "state": "AL",
                  "postalCode": "35805",
                  "country": "USA",
                  "dayPhone": "12344",
                  "emailId": "[email protected]"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

2 Answers 2

0

You can try with below code:

$assocArray = json_decode($jsonData, true);

Hope this solution will help you!

Happy Coding!

4
  • Faisal, what is the use of 'true' as one of the parameters for json_decode? Commented Jul 27, 2020 at 7:23
  • Here second parameter "true" is set to convert json data as an associative array and if we set parameter "false" or "default" then both parameters set the result as an object. Hope you understand. Commented Jul 27, 2020 at 7:35
  • Yes, clarified. Commented Jul 27, 2020 at 7:46
  • If you found this solution is working fine, so please also give a upvote to it. Commented Jul 27, 2020 at 7:56
2

This also what I am using everywhere it's working.

You could use \Magento\Framework\Serialize\Serializer\Json instead.

Ie.

public function __construct(
        \Magento\Framework\Serialize\Serializer\Json $json
    ) {
        $this->json = $json;
    }

 public function yourFunction()
 {
   $jsonDecode = $this->json->unserialize($result);

   $json = $this->json->serialize($jsonDecode);

 }

Or You can use this one

Found how to achieve this:

Use the following to convert the JSON into an array:

\Magento\Framework\Webapi\Rest\Request\Deserializer\Json\Deserializer::deserialize('jsonString')

Use the following to convert to an object (Path to class is the class that the object will be processed by)

\Magento\Framework\Webapi\ServiceInputProcessor::process('\Path\To\Class', 'methodName', $deserializedJson)
2
  • Will definitely use this piece of code when I am in a similar situation on the Magento side. I'm now working with an application that is written purely in PHP which is connected to our Magento site through event/observers. Thanks for the help. Commented Jul 27, 2020 at 7:06
  • 1
    Okay, If you doing pure PHP code then you can easily use $array = json_decode($jsonFormatdate, true); It's Working, I was things if you doing development in Magento that's why I have provided you Magento standard code.
    – Tushar
    Commented Jul 27, 2020 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.