0

When i received the message in json format as below:

{
  "Details":{
      "Type":"Cash",
      "Payid":"PAY123456",
      "Amount":"9000,00",
      "Status":"Successful",
  }
}

I need to return response in the following JSON format with two parameters only (Type & Payid) that received

{
  "Details": {
    "Type": "Cash",
    "Payid": "PAY123456"
    }
}

Currently in my controller it will return the whole details that received. But how do I modified it, so that it will just return the certain parameters.

public function returnResponse(Request $request)
{
    $datas= $request->getContent();
    $returnData= json_decode($datas, true);
    return response()->json($returnData);
}
10
  • 1
    It's not so surprising because you do not modify the content $datas before sending it as JSON. Try debugging what exactly is in $datas, modify its content and then send it as JSON. Commented Sep 18, 2017 at 2:31
  • hi @marekful, may i know how to modified the content $datas? the content $datas is { "Details":{ "Type":"Cash", "Payid":"PAY123456", "Amount":"9000,00", "Status":"Successful", } } Commented Sep 18, 2017 at 2:34
  • There is more than one way of doing it. It mostly depends on how reliable is the received data. You could use unset($returnData['Details']['Amount']); unset($returnData['Details']['Status']); after decoding it. Commented Sep 18, 2017 at 2:36
  • Assuming it is an instance of stdClass, you can unset($datas->Details->Amount), etc... Commented Sep 18, 2017 at 2:36
  • @marekful second argument in json_decode means array ;) (And since they are using json_decode, we could assume $datas is just a string) Commented Sep 18, 2017 at 2:37

2 Answers 2

2

This is one way to do it:

public function returnResponse(Request $request){
    $datas = $request->getContent();
    $parsedJson = json_decode($datas, true);
    $returnData = array('Details' => array(
        'Type' => $parsedJson['Details']['Payid'],
        'Payid' => $parsedJson['Details']['Payid'],
        'Confirmation' => 0 // do the same in this line if you want to add more
    ));
    return response()->json($returnData);
}

This code assumes you'll always have 'Type' and 'Payid' inside 'Details'.

The result will be:

Array
(
    [Details] => Array
        (
            [Type] => PAY123456
            [Payid] => PAY123456
            [Confirmation] => 0
        )

)

Note, if you just want to add Confirmation to the original array, you can use:

$returnData['Details']['Confirmation'] = 0;
Sign up to request clarification or add additional context in comments.

Comments

0

You can also hide the parameter that you don't want your customer or user to see go to the Model file and

protected $hidden=[
// here you will type the property name like
"update_at",
]

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.