0

I have a json response how do i echo the response in message i tried json_encode but it seems not working

{
    "success": "verification_error",
    "message": [
        "required.",
    ]
}
Alexandre B.
  • 5,387
  • 2
  • 17
  • 40
Mema Skie
  • 9
  • 2

1 Answers1

0

I think you mix json_encode and json_decode.

  • json_encode is used to create a json object (from an array for example).

  • json_decode do the opposite: decode a json object.

Another point is that the message field is an array. You can not directly display array as string.

  1. In your example, you only have one element in the array. So you can directly display it by selecting it (with index).

  2. If the array has more than one element, you can either iterate over the array and apply the first case or implode the array to a string. Some discussion about the question here.

Here an example:

<?php

$a = array("success"=> "verification_error",
    "message"=> [
        "required.",
    ]
);

$json_response = json_encode($a);
echo("Your response (json encoded): ".$json_response);
// Your response (json encoded): {"success":"verification_error","message":["required."]}

$data = json_decode($json_response, true);
echo("<br/>Message: ".$data["message"][0]);
// Message: required.
?>
Alexandre B.
  • 5,387
  • 2
  • 17
  • 40