Skip to content

Conversation

@rvadera12
Copy link
Contributor

@rvadera12 rvadera12 commented Oct 24, 2025

Problem
Customers encountered an error when using the JSON decode function in destination mappings with primitive string values (e.g., plain text, numbers, or booleans as strings) rather than valid JSON strings.

When this occurred, users would see a "Could not load page" error because JSON.parse() would throw an error when attempting to parse non-JSON strings like plain text values.

Solution
Added error handling to the @JSON directive's decode mode in the mapping-kit to gracefully handle invalid JSON strings, similar to how the @case directive handles non-string inputs.

Changes:

Wrapped JSON.parse(value) in a try-catch block in packages/core/src/mapping-kit/index.ts
When JSON parsing fails, the directive now returns the original string value instead of throwing an error
This allows primitive string values to pass through unchanged rather than breaking the mapping

Example:

Before: Would throw error
{ '@JSON': { mode: 'decode', value: 'Hello World' } }

After: Returns original string
{ '@JSON': { mode: 'decode', value: 'Hello World' } } // Returns: 'Hello World'

Testing
Added a comprehensive unit test decode_invalid_json_returns_original_string in index.iso.test.ts that verifies:

Non-JSON strings are returned as-is without throwing errors
The decode functionality still works correctly for valid JSON strings
The error handling doesn't break existing functionality
This change ensures backward compatibility while preventing user-facing errors when non-JSON values are accidentally passed to the decode function.

More information: https://twilio-engineering.atlassian.net/browse/ACTVTNS-387

Testing

Screen.Recording.2025-10-24.at.5.28.03.PM.mov

](url)

Include any additional information about the testing you have completed to
ensure your changes behave as expected. For a speedy review, please check
any of the tasks you completed below during your testing.

  • [x ] Added unit tests for new functionality
  • Tested end-to-end using the local server
  • [If destination is already live] Tested for backward compatibility of destination. Note: New required fields are a breaking change.
  • [Segmenters] Tested in the staging environment
  • [Segmenters] [If applicable for this change] Tested for regression with Hadron.

@github-actions
Copy link
Contributor

This PR makes changes to mapping-kit. Please ensure that the changes are reflected in the mapping-kit go library as well and link the PR in description.

@codecov
Copy link

codecov bot commented Oct 24, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.34%. Comparing base (ce524ca) to head (779da28).
⚠️ Report is 22 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3368      +/-   ##
==========================================
+ Coverage   79.99%   80.34%   +0.34%     
==========================================
  Files        1207     1260      +53     
  Lines       22315    25168    +2853     
  Branches     4398     5149     +751     
==========================================
+ Hits        17851    20220    +2369     
- Misses       3688     4166     +478     
- Partials      776      782       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@rvadera12 rvadera12 changed the title update JSON function [ACTVTNS-387] JSON Decode Error Oct 24, 2025
@marinhero marinhero requested a review from Copilot October 24, 2025 21:34
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a JSON decode error in the mapping-kit where non-JSON primitive string values (like plain text, numbers, or booleans as strings) would cause JSON.parse() to throw an error, resulting in a "Could not load page" error for users.

Key Changes:

  • Added try-catch error handling to the @json directive's decode mode to gracefully handle invalid JSON strings
  • Invalid JSON strings now return the original value instead of throwing an error
  • Added unit test to verify the fix works correctly

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
packages/core/src/mapping-kit/index.ts Added try-catch block around JSON.parse() to handle invalid JSON gracefully and return original value on parse failure
packages/core/src/mapping-kit/tests/index.iso.test.ts Added test case to verify that invalid JSON strings are returned as-is without throwing errors

} else if (opts.mode === 'decode') {
if (typeof value === 'string') {
return JSON.parse(value)
//Placing this in a try-catch to safely handle any primiative values that are put in the JSON.parse
Copy link

Copilot AI Oct 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected spelling of 'primiative' to 'primitive'.

Suggested change
//Placing this in a try-catch to safely handle any primiative values that are put in the JSON.parse
//Placing this in a try-catch to safely handle any primitive values that are put in the JSON.parse

Copilot uses AI. Check for mistakes.
} else if (opts.mode === 'decode') {
if (typeof value === 'string') {
return JSON.parse(value)
//Placing this in a try-catch to safely handle any primiative values that are put in the JSON.parse
Copy link

Copilot AI Oct 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment should follow standard formatting with a space after the double slashes: // Placing this in a try-catch... Additionally, consider making the comment more precise about what primitive values are being handled (e.g., 'non-JSON strings').

Suggested change
//Placing this in a try-catch to safely handle any primiative values that are put in the JSON.parse
// Placing this in a try-catch to safely handle non-JSON strings or primitive values that may cause JSON.parse to throw

Copilot uses AI. Check for mistakes.
@joe-ayoub-segment joe-ayoub-segment merged commit c10e5bc into main Nov 5, 2025
14 checks passed
@joe-ayoub-segment joe-ayoub-segment deleted the ACTVTNS-387-JSON-decode-error branch November 5, 2025 05:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants