jleyva / moodle-block_configurablereports

This block is a Moodle custom reports builder. You can create custom reports without SQL knowledge. It's a tool suitable for admins or teachers.
http://moodle.org/plugins/view.php?plugin=block_configurable_reports
68 stars 159 forks source link

JSON Accessed via Web Service Contains Escape Characters #188

Open thebenkahn opened 3 years ago

thebenkahn commented 3 years ago

Hello, when using the block_configurable_reports_get_report_data function to access report data in JSON format, the payload contains JSON escape characters that can issues when a client parses the data. If possible it would be great if the data could be cleaned prior to being returned to the client.

Example of JSON downloaded via the front-end export as JSON option:

[{"firstname":"Bart","lastname":"Simpson","city":"Springfield"},{"firstname":"Lisa","lastname":"Simpson","city":""},{"firstname":"Homer","lastname":"Simpson","city":""}]

Same data accessed via the web service:

{
    "data": "[\n    {\n        \"firstname\": \"Bart\",\n        \"lastname\": \"Simpson\",\n        \"city\": \"Springfield\"\n    },\n    {\n        \"firstname\": \"Lisa\",\n        \"lastname\": \"Simpson\",\n        \"city\": \"\"\n    },\n    {\n        \"firstname\": \"Homer\",\n        \"lastname\": \"Simpson\",\n        \"city\": \"\"\n    }\n]",
    "warnings": ""
}

Thanks!

ertborTek commented 1 year ago

Note that setting the moodlewssettingraw parameter to true also doesn't prevent the escaping.

ertborTek commented 1 year ago

From what I can tell, the get_report_data function uses the JSON_PRETTY_PRINT flag when preparing JSON output with json_encode(). This is where the escaped newlines (\n) come from. However, as for the escaped quotes, it seems that data is first JSON encoded by Configurable Reports (via get_report_data()) and then again by Web Services. The JSON string passed back to Web Services is treated like an ordinary text value and escaped like one.

mgleeson commented 1 year ago

Note also there appears to be a 3rd element to the issue as well perhaps (or having the same root cause as the escaped quotes possibly). ie. in addition to the escaped newlines and escaped double-quotes there's an extra set of double-quotes surrounding the content of the "data" element, specifically around the array brackets.

(See below, I've used @thebenkahn's example JSON, sans the newlines and escaped double-quotes for clarity of illustration)


   "data":"[ 
       { 
            "firstname": "Bart", 
            "lastname": "Simpson", 
            "city": "Springfield" 
        }, 
        { 
            "firstname": "Lisa", 
            "lastname": "Simpson", 
            "city": "" }, 
        { 
            "firstname": "Homer", 
            "lastname": "Simpson", 
            "city": "" 
        }
 ]",
   "warnings":""
}