1

I am getting following response in JSON format and I want it to convert it into PHP variables.

JSON:

{"CreateTransactionResponse":{"CreateTransactionResult":{"TransportKey":"aa900d54-7bfb-47e9-a5de-e423ec34a900"
,"ValidationKey":"fbb28b32-f439-4801-a434-99c70aa388ca","Messages":{}}}}

output should be PHP:

$transkey = aa900d54-7bfb-47e9-a5de-e423ec34a900;
$vkey = fbb28b32-f439-4801-a434-99c70aa388ca

please advice me how to do it.

1
  • simplest way posted, mark as answer if its what you are trying to find. So that everyone who visit in search of the answer for same question will see what worked for you best.anyway best of luck :) Commented Sep 7, 2015 at 8:40

5 Answers 5

1

Just simply use json_decode();

 $result= json_decode($jSon);

 var_dump($result); // to see the output
Sign up to request clarification or add additional context in comments.

1 Comment

Now if i want some specific values from dump then how we can get these values?
1

json to array(json_decode) and then extract from array.

$arr = json_decode($json, true);
extract($arr);
var_dump($CreateTransactionResponse);

Output:

array (size=1)
  'CreateTransactionResult' => 
    array (size=3)
      'TransportKey' => string 'aa900d54-7bfb-47e9-a5de-e423ec34a900' (length=36)
      'ValidationKey' => string 'fbb28b32-f439-4801-a434-99c70aa388ca' (length=36)
      'Messages' => 
        array (size=0)
          empty

More about extract

use $CreateTransactionResult['TransportKey'] to access Transport Key from JSON. Similarly $CreateTransactionResult['ValidationKey'] for Validation Key.

Comments

0

If you want to access your json try to decode it first:

$result = json_decode($yourJSON, true);

foreach($result['CreateTransactionResponse'] as $key => $val){
   echo $transkey = 'TransportKey= ' . $val['TransportKey'] . '<br/>;
   echo $vkey = 'ValidationKey= ' . $val['ValidationKey'];
}

Or if it is an array of JSON's

$result = json_decode($yourJSON, true);

$data = [];
foreach($result['CreateTransactionResponse'] as $key => $val){
   $data[] = [
        'TransportKey' => $val['TransportKey'],
        'ValidationKey' => $val['ValidationKey']
   ];
}
print_r($data);

Comments

0
    try this code it will work  
$JSON='{"CreateTransactionResponse":{"CreateTransactionResult":{"TransportKey":"aa900d54-7bfb-47e9-a5de-e423ec34a900" ,"ValidationKey":"fbb28b32-f439-4801-a434-99c70aa388ca","Messages":{}}}}';    

    $arr=json_decode($JSON, TRUE);
    foreach ($arr as  $value) {

    foreach ($arr['CreateTransactionResponse'] as $key => $var) {
        echo 'TransportKey = '.$var['TransportKey'].'<br>';
        echo 'ValidationKey = '.$var['ValidationKey'].'<br>';
        foreach ($var['Messages'] as $key => $msg) {


        echo 'Messages = '.$msg.'<br>';
    }

        }
    }

Comments

0

In this case,If its a single and TransportKey and a single ValidationKey value (not an array/object is passed) at a time, this is the simplest. Else if object contains objects or inside objects that we want to use or convert to variable, should use a foreach to loop through the object.

//Debuggig
//The string you provided is converted to a json object 
//In your case if it is a json object already pass directly it to $j
//below is just for debugging and understanding
//$json='{"CreateTransactionResponse":{"CreateTransactionResult":{"TransportKey":"aa900d54-7bfb-47e9-a5de-e423ec34a900","ValidationKey":"fbb28b32-f439-4801-a434-99c70aa388ca","Messages":{}}}}';
//$j=json_decode($json);

$transkey=$j->CreateTransactionResponse->CreateTransactionResult->TransportKey;
$vkey=$j->CreateTransactionResponse->CreateTransactionResult->ValidationKey;

echo $transkey."</br>";
echo $vkey."<br/>";
/*result as said: 
aa900d54-7bfb-47e9-a5de-e423ec34a900
fbb28b32-f439-4801-a434-99c70aa388ca
*/

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.