Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to have multiple JSON data in a single parameter?

I want to encode JSON data with php but failed. i have a property which is having number of parameters but i do not know how to do it. I tried severally but failed.
Am new to JSON.
I want to put parameters in general_params property as shown below but failed.
i tried this

$validated=true;
$message='Payment made';
$general_params='{"name": "Ali Mercy","amount": "10000","txrefid": "1234567"}';
response($validated,$message,$general_params);
function response($validated,$message,$general_params){
    
    $response['validated'] = $validated;
    $response['message'] = $message;
    $response['general_params'] = $general_params;
    
    $json_response = json_encode($response);
    echo $json_response;
}

I want the output to be like this below but all failed

{
"validated": true,
"message":"Payment made",
"general_params": {"name": "Ali Mercy","amount": "10000","txrefid": "1234567"}
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

$general_params should be an array, not a string.

$general_params = ["name" => "Ali Mercy", "amount" => "10000", "txrefid" => "1234567"];

If you’ve been given it as a string, you can use json_decode() to parse it.

$general_params=json_decode('{"name": "Ali Mercy","amount": "10000","txrefid": "1234567"}');
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading