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
You can try with below code:
$assocArray = json_decode($jsonData, true);
Hope this solution will help you!
Happy Coding!
-
Faisal, what is the use of 'true' as one of the parameters for json_decode?CodeForGood– CodeForGood2020年07月27日 07:23:01 +00:00Commented 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.Faisal Sheikh– Faisal Sheikh2020年07月27日 07:35:16 +00:00Commented Jul 27, 2020 at 7:35
-
Yes, clarified.CodeForGood– CodeForGood2020年07月27日 07:46:42 +00:00Commented Jul 27, 2020 at 7:46
-
If you found this solution is working fine, so please also give a upvote to it.Faisal Sheikh– Faisal Sheikh2020年07月27日 07:56:26 +00:00Commented Jul 27, 2020 at 7:56
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)
-
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.CodeForGood– CodeForGood2020年07月27日 07:06:48 +00:00Commented Jul 27, 2020 at 7:06
-
1Okay, 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– Tushar2020年07月27日 11:46:30 +00:00Commented Jul 27, 2020 at 11:46