0

I have a Json as follows in which I 've to get only transId inside response in php. please help as I am new to php

{
    "type": null,
    "requestuid": null,
    "orderId": "anand12345",
    "status": "SUCCESS",
    "statusCode": "SUCCESS",
    "statusMessage": "SUCCESS",
    "response": {
        "transId": "1408544"
    },
    "metadata": "Testing Data"
}
executable
  • 3,365
  • 6
  • 24
  • 52
Anantha Raman
  • 197
  • 2
  • 11
  • you want to extract `transId`. is that what you mean? – oreopot Nov 30 '18 at 10:29
  • Yes, I exactly want to get transId – Anantha Raman Nov 30 '18 at 10:30
  • 1
    You might need to look at this function http://php.net/manual/fr/function.json-decode.php . Not that a boolean can be passed as second argument to force the conversion to produce an associative array or a StdClass – Zyigh Nov 30 '18 at 10:31
  • 1
    _Suggestion:_ Before posting, make sure you have done proper research. It would have been pretty easy to find this information since it's been asked and answered many many times. – M. Eriksson Nov 30 '18 at 10:32

2 Answers2

3
$json = '{
 "type": null,
 "requestuid": null,
 "orderId": "anand12345",
 "status": "SUCCESS",
 "statusCode": "SUCCESS",
 "statusMessage": "SUCCESS",
 "response": {"transId": "1408544"},
 "metadata": "Testing Data"
}';

$arr = json_decode($json,true);
$transId = $arr['response']['transId'];
oreopot
  • 3,392
  • 2
  • 19
  • 28
1

read your file and store its content as a string (here i directly declared a string stored in $json ) using json_decode php function you get an object with all your data and just have to use field names to access your data

$json = '{
"type": null,
"requestuid": null,
"orderId": "anand12345",
"status": "SUCCESS",
"statusCode": "SUCCESS",
"statusMessage": "SUCCESS",
"response": {
    "transId": "1408544"
},
"metadata": "Testing Data"

}';


$decoded = json_decode($json);
echo $decoded->response->transId;
RomMer
  • 113
  • 12