-1

I have a JSON variable and can't get the data in the second dimension. How can I retrieve for example the value of the key='TransactionAmount' in the below code using php?

I can retrieve the transactionid, resultdescription.

$json = {
    "Result": {
        "ResultType": 0,
        "ResultCode": 0,
        "ResultDesc": "The service request is processed successfully.",
        "OriginatorConversationID": "2159-1931249-1",
        "ConversationID": "AG_20190925_0000425ed57b5e1fee9b",
        "TransactionID": "NIP21HAIT2",
        "ResultParameters": {
            "ResultParameter": [{
                "Key": "TransactionReceipt",
                "Value": "NIP21HAIT2"
            }, {
                "Key": "TransactionAmount",
                "Value": 500
            }, {
                "Key": "B2CWorkingAccountAvailableFunds",
                "Value": 350000.00
            }, {
                "Key": "B2CUtilityAccountAvailableFunds",
                "Value": 5134.00
            }, {
                "Key": "TransactionCompletedDateTime",
                "Value": "25.09.2019 18:30:23"
            }, {
                "Key": "ReceiverPartyPublicName",
                "Value": "254708374149 - John Doe"
            }, {
                "Key": "B2CChargesPaidAccountAvailableFunds",
                "Value": -440.00
            }, {
                "Key": "B2CRecipientIsRegisteredCustomer",
                "Value": "Y"
            }]
        },
        "ReferenceData": {
            "ReferenceItem": {
                "Key": "QueueTimeoutURL",
                "Value": "https:\/\/internalsandbox.safaricom.co.ke\/mpesa\/b2cresults\/v1\/submit"
            }
        }
    }
}
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • Hi! To ask a question which helps people to help you, and is useful to future readers, try to include a [mcve] - as well as a simplified block of JSON, show us what you've tried so far, and where you're struggling. – IMSoP Sep 25 '19 at 16:24

2 Answers2

-1

First of off, use json_decode(YOUR_JSON_STRING, true) and then you can access to it:

$json = json_decode("your json", true);
$json["Result"]["TransactionID"] or $json["Result"]["ResultDesc"]

More about json_decode here: https://www.php.net/manual/en/function.json-decode.php

Simon
  • 179
  • 1
  • 11
-1

First, you need to decode your JSON String using json_decode() method.

$jsonString = '{"Result":{"ResultType":0,"ResultCode":0,"ResultDesc":"The service request is processed successfully.","OriginatorConversationID":"2159-1931249-1",
"ConversationID":"AG_20190925_0000425ed57b5e1fee9b","TransactionID":"NIP21HAIT2","ResultParameters":{"ResultParameter":[{"Key":"TransactionReceipt",
"Value":"NIP21HAIT2"},{"Key":"TransactionAmount","Value":500},{"Key":"B2CWorkingAccountAvailableFunds","Value":350000.00},{"Key":"B2CUtilityAccountAvailableFunds",
"Value":5134.00},{"Key":"TransactionCompletedDateTime","Value":"25.09.2019 18:30:23"},{"Key":"ReceiverPartyPublicName","Value":"254708374149 - John Doe"},
{"Key":"B2CChargesPaidAccountAvailableFunds","Value":-440.00},{"Key":"B2CRecipientIsRegisteredCustomer","Value":"Y"}]},"ReferenceData":{"ReferenceItem":
{"Key":"QueueTimeoutURL","Value":"https://internalsandbox.safaricom.co.ke/mpesa/b2cresults/v1/submit"}}}}';

$resultArray = json_decode($jsonString , true); // json_decode() will return an object or array if second value it's true

echo $resultArray["Result"]["ResultCode"]; // Print the ResultCode
echo $resultArray["Result"]["OriginatorConversationID"]; // Print the OriginatorConversationID
echo $resultArray["Result"]["ResultDesc"]; // Print the ResultDesc

And as you asked you need to get the data inside ResultParameters and it is also an array. So to get the data inside that array,

echo $resultArray["Result"]["ResultParameters"]["ResultParameter"][0]["Key"]; // Print the Key
echo $resultArray["Result"]["ResultParameters"]["ResultParameter"][0]["Value"]; // Print the Value

So in the above 0 is the first element in the array. You have to iterate over all array elements to get all the Key-Value pairs.

And as it is not the best way to get data inside an array, you can iterate the data using for loop as follows,

for ($i = 0; $i < count($resultArray["Result"]["ResultParameters"]["ResultParameter"]); $i++) {
    echo $resultArray["Result"]["ResultParameters"]["ResultParameter"][$i]["Key"];
    echo " ";
    echo $resultArray["Result"]["ResultParameters"]["ResultParameter"][$i]["Value"];
    echo "\n";
}

And the printed data will be,

TransactionReceipt NIP21HAIT2
TransactionAmount 500
B2CWorkingAccountAvailableFunds 350000 
B2CUtilityAccountAvailableFunds 5134 
TransactionCompletedDateTime 25.09.2019 18:30:23
ReceiverPartyPublicName 254708374149 - John Doe
B2CChargesPaidAccountAvailableFunds -440
B2CRecipientIsRegisteredCustomer Y

Hope this helps you.

Hasitha Jayawardana
  • 2,326
  • 4
  • 18
  • 36