Page 1 of 1

JSON.parse error when try to load a Report from Database

Posted: Mon Mar 07, 2022 2:59 am
by harzmann
Hello Support Team,

I am saving a report file to a database using the 'saveToJsonString' command:
==================================================================
designer.onSaveReport = function (e) {
var jsonStr = e.report.saveToJsonString();
...
}

Then I try to load the stored string from Database to the Designer again:
==============================================================
// Create a new report instance
var report = new Stimulsoft.Report.StiReport();
// Load report from json string
report.load(report_json);
// Edit report template in the designer
designer.report = report;

We use Python Flask and invoke the HTML site like this:
================================================
@dlc_v_dlc_analysis.route('/designer_json')
@login_required
def select_v_dlc_analysis_designer_json():
cur = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
result = cur.execute(
"SELECT id, uuid, last_name, first_name, address, zip_code, city, country, email, phone, "
"IFNULL(DATE_Format(date_entry, '%Y-%m-%d'), '') AS 'date_entry', "
"IFNULL(DATE_Format(date_exit, '%Y-%m-%d'), '') AS 'date_exit', "
"license_nr, rfid, check_interval, authority, "
"IFNULL(DATE_Format(date_issue, '%Y-%m-%d'), '') AS 'date_issue', "
"IFNULL(DATE_Format(date_valid, '%Y-%m-%d'), '') AS 'date_valid', "
"IFNULL(DATE_Format(last_control_time, '%Y-%m-%d'), '') AS 'last_control_time', "
"IFNULL(DATE_Format(next_control_time, '%Y-%m-%d'), '') AS 'next_control_time', "
"IFNULL(due_in_days, 0) AS 'due_in_days', "
"remarks, device_id "
"FROM v_dlc_analysis ORDER BY last_name, id")
select_v_dlc_analysis = cur.fetchall()
result = cur.execute("SELECT report_json FROM reports WHERE report_name='dlc_user1'")
report_json = cur.fetchone()

return render_template('dlc_report_designer_json.html', staff=select_v_dlc_analysis, report_json=report_json)

At the HTML-site we parse the JSON Data like this:
============================================
// Load data from JSON
var staff = JSON.parse('{{staff | tojson | safe}}');
console.log(staff)
var report_json = JSON.parse('{{report_json | tojson}}');
console.log(report_json)

At this point I receive the error
Uncaught SyntaxError: Unexpected token
in JSON at position 18
at JSON.parse (<anonymous>)
when invoking
var report_json = JSON.parse('{{report_json | tojson}}');

When I compare both JSON strings - the one from the function saveToJsonString() and the one stored in the database - they are exactly the same. Can you give me a hint whats the problem is?

Thanks in advance.
Greetings Joerg

Re: JSON.parse error when try to load a Report from Database

Posted: Mon Mar 07, 2022 10:00 pm
by Lech Kulikowski
Hello,

Please send us a sample project that reproduces the issue for analysis.

Thank you.

Re: JSON.parse error when try to load a Report from Database

Posted: Tue Mar 08, 2022 1:05 am
by harzmann
Hi Support Team,

I solved the problem. The problem was the assigning of the String-Value which contains the Report-Json-String. My solution is:

In Flask Python:
==============
cur = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
result = cur.execute("SELECT report_json FROM reports WHERE report_name='dlc_user1'")
row = cur.fetchone()
report_json_str = row['report_json']
return render_template('kendo/dlc_v_dlc_reports_kd.html', report=report_json_str)

In HTML Template:
================
var report_json = {{report | tojson | safe }};
report.load(report_json);

Thanks Joerg

Re: JSON.parse error when try to load a Report from Database

Posted: Tue Mar 08, 2022 9:23 am
by Lech Kulikowski
Hello,

Thank you for the information.