Skip to content

Commit

Permalink
Fix JSON deserialization error in render_j2_template function
Browse files Browse the repository at this point in the history
  • Loading branch information
mjanez committed Sep 24, 2024
1 parent 80ab7d2 commit 8f3af39
Showing 1 changed file with 7 additions and 23 deletions.
30 changes: 7 additions & 23 deletions ckan2pycsw/model/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,34 +116,18 @@ def render_j2_template(mcf: dict, schema_type: str, url: str = None, template_di
LOGGER.error(msg)
raise RuntimeError(msg)

LOGGER.debug('Processing CKAN template to JSON')
mcf = update_object_lists(mcf)

try:
LOGGER.debug('Processing CKAN template to JSON')
mcf = update_object_lists(mcf)

# Render the template and directly attempt to correct and deserialize the JSON string
rendered_template = template.render(record=mcf)

# Remove trailing commas if any (not recommended, better to correct the template).
sanitized_json = re.sub(r',\s*([\]}])', r'\1', rendered_template)

# JSON deserialize
mcf_dict = json.loads(sanitized_json, strict=False)
mcf_dict = json.loads(re.sub(r'\\(?!["\\/bfnrtu])', r'\\\\', template.render(record=mcf)), strict=False)
except json.JSONDecodeError as e:
LOGGER.error("Error deserializing the template output: %s", e)

# Optional: Save the generated JSON before deserialisation for debugging purposes.
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
error_file_name = f'error_rendered_output_{timestamp}.json'
debug_directory = LOG_DIR / 'errors'
os.makedirs(debug_directory, exist_ok=True)
error_file_path = os.path.join(debug_directory, error_file_name)

# Optional: Save the generated JSON before deserialisation for debugging purposes.
with open(error_file_path, 'w', encoding='utf-8') as f:
f.write(rendered_template)
LOGGER.error("Problematic output saved to: %s", error_file_path)
# Optionally: Save the problematic output for debugging
LOGGER.error("Problematic output: %s", template.render(record=mcf))
raise

return mcf_dict

if schema_type == 'pygeometa':
Expand Down

0 comments on commit 8f3af39

Please sign in to comment.