0

I have the below JSON data in server. How I can extract only "SUCCESS" as output in PHP, where SUCCESS is result of Status which is inside object of data. i.e "status":"SUCCESS",

{  
   "response":{  
      "status":true,
      "statusCode":"0",
      "statusDescription":"Amount Debited",
      "data":{  
         "balanceamount":"528",
         "status":"SUCCESS",
         "statuscode":"0",
         "statusdescription":"Amount Debited",
         "debitedamount":"2",
         "orderid":"hj",
         "refId":"4450"
      }
   },
   "checksum":"23e97234820f5987189245e4216e89425472c2fe2c957749743b21d828efae67"
}

UPDATE

$ch = curl_init();  // initiate curl
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);  // tell curl you want to post something
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string); // define what you want to post
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return the output in string format
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);     
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$output = curl_exec ($ch); // execute

//echo "Request_Json= ".$data_string;
//echo "Response_Json=".$output;
echo $output;

This $output gives me the above JSON. But when I use your answer I get output empty. Anything I'm doing wrong ?

Pradeep
  • 9,667
  • 13
  • 27
  • 34
GoGoaGone
  • 11
  • 3
  • What have you tried so far? Take a look at the following tutorial. https://www.taniarascia.com/how-to-use-json-data-with-php-or-javascript/ – Will_Panda May 03 '18 at 06:28
  • 1
    Possible duplicate of [How do I extract data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – apokryfos May 03 '18 at 06:34

2 Answers2

1

hope this will work for you :

use json_decode for json string

$json= '{  
   "response":{  
      "status":true,
      "statusCode":"0",
      "statusDescription":"Amount Debited",
      "data":{  
         "balanceamount":"528",
         "status":"SUCCESS",
         "statuscode":"0",
         "statusdescription":"Amount Debited",
         "debitedamount":"2",
         "orderid":"hj",
         "refId":"4450"
      }
   },
   "checksum":"23e97234820f5987189245e4216e89425472c2fe2c957749743b21d828efae67"
}';


$arr = json_decode($json);

//print_r($arr);
echo $arr->response->data->status;
/*Output SUCCESS*/
die;

UPDATE :

 $output = curl_exec ($ch); 
 $arr = json_decode($output );
 echo $arr->response->data->status;

for more : http://php.net/manual/en/function.json-decode.php

Pradeep
  • 9,667
  • 13
  • 27
  • 34
  • appreciate your answer @pradeep , will you please look at my new edit and help me out. – GoGoaGone May 03 '18 at 06:40
  • replace $json of my answer with $output,if the $output has same string as shown in the question u will definitely get the answer. – Pradeep May 03 '18 at 06:44
  • its ok you downvote this but why down vote ? please explain, may be it is because of a right answer for the question – – Pradeep May 03 '18 at 08:27
  • Its not a wrong answer. But It doesn't resolve my issue. No worries, I didn't downvoted but there are others who did my question too. :( – GoGoaGone May 04 '18 at 07:06
  • i think this resolve your initial question, after updating question it changes the scenario that was another problem , by the way its ok – Pradeep May 04 '18 at 07:11
0
$output = '{  
   "response":{  
      "status":true,
      "statusCode":"0",
      "statusDescription":"Amount Debited",
      "data":{  
         "balanceamount":"528",
         "status":"SUCCESS",
         "statuscode":"0",
         "statusdescription":"Amount Debited",
         "debitedamount":"2",
         "orderid":"hj",
         "refId":"4450"
      }
   },
   "checksum":"23e97234820f5987189245e4216e89425472c2fe2c957749743b21d828efae67"
}';
 $json_result = json_decode($output);
echo $json_result->response->data->status;
Rp9
  • 1,955
  • 2
  • 23
  • 31