0

I am creating a webservice in moodle and want to return my std object as a JSON,

public static function add_attendance_lti($attendanceObject)
    {
......
$lti_updated = [
            'id' => $insert_new_attendance,
            'code' => $statusCode,
            'message' => $message,
            'data' => json_encode($AttendanceLog)
        ];
        return $lti_updated;
  }

public static function add_attendance_lti_returns()
    {
        return new external_single_structure(
            array(
                'id' => new external_value(PARAM_RAW, 'New Attendance id'),
                'code' => new external_value(PARAM_INT, 'status code of response'),
                'message' => new external_value(PARAM_TEXT, 'message returned'),
                'data' => new external_value(PARAM_RAW, 'Return Value')
            )
        );
    }

Here i am converting the $AttendanceLog std object to JSON using json_encode(), but this returns the following data,

{
    "id": null,
    "code": 200,
    "message": "Attendance Record already exists",
    "data": "{\"sessionid\":\"18\",\"timetaken\":1643335205,\"studentid\":\"4\",\"takenby\":\"3\",\"statusset\":\"49,51,52,50\",\"statusid\":\"52\"}"
}

How can i return a normal "data" JSON object without the \"

  • Are you `json_encode()`’ing `$lti_leave` as well? Why would you need to do this twice…? – esqew Jan 26 '22 at 13:28
  • $AttendanceLog is a std object so i am converting it to JSON using json_encode(), i don't understand what you mean by json_encode()’ing $lti_leave. – Syed Saad Ullah Shah Jan 26 '22 at 13:31

2 Answers2

1

You are encoding to json your string in add_attendance_lti with json_encode, and the resulting string will be escaped in add_attendance_lti_returns. Hence this slashed encoding.

You can try to convert your std object to an array in add_attendance_lti :

public static function add_attendance_lti($attendanceObject)
    {
......
$lti_updated = [
            'id' => $insert_new_attendance,
            'code' => $statusCode,
            'message' => $message,
            'data' => json_decode(json_encode($AttendanceLog), true)
        ];
        return $lti_updated;
  }

(see php stdClass to array)

That way, this method will return an array, and the value of the json for the key "data" at the other end will be a json - not an escaped string.

Cedric
  • 5,135
  • 11
  • 42
  • 61
  • i get this error using your solution, { "exception": "invalid_response_exception", "errorcode": "invalidresponse", "message": "Invalid response value detected" } – Syed Saad Ullah Shah Jan 27 '22 at 05:45
0

Pass $AttendanceLog variable without using json_encode in data key and after that convert $lti_updated with json_encode like this

json_encode($lti_updated); 

in return like

return json_encode($lti_updated);
Dula
  • 1,276
  • 5
  • 14
  • 23
  • i get this error using your solution, { "exception": "invalid_response_exception", "errorcode": "invalidresponse", "message": "Invalid response value detected" } – Syed Saad Ullah Shah Jan 27 '22 at 05:44