1- import * as jsonpathModule from 'jsonpath' ;
1+ import { JSONPath } from 'jsonpath-plus ' ;
22import {
33 GenericEvaluationContext ,
44 InSegmentCondition ,
@@ -10,9 +10,6 @@ import { getHashedPercentageForObjIds } from '../utils/hashing/index.js';
1010import { SegmentConditionModel } from './models.js' ;
1111import { IS_NOT_SET , IS_SET , PERCENTAGE_SPLIT } from './constants.js' ;
1212
13- // Handle ESM/CJS interop - jsonpath exports default in ESM
14- const jsonpath = ( jsonpathModule as any ) . default || jsonpathModule ;
15-
1613/**
1714 * Returns all segments that the identity belongs to based on segment rules evaluation.
1815 *
@@ -140,8 +137,22 @@ function evaluateRuleConditions(ruleType: string, conditionResults: boolean[]):
140137 }
141138}
142139
140+ const TRAITS_DOT_PATTERN = / ^ \$ \. i d e n t i t y \. t r a i t s \. ( .+ ) $ / ;
141+ const TRAITS_BRACKET_PATTERN = / ^ \$ \. i d e n t i t y \. t r a i t s \[ ' ( .+ ) ' \] $ / ;
142+
143+ function extractTraitNameFromPath ( property : string ) : string | undefined {
144+ return TRAITS_DOT_PATTERN . exec ( property ) ?. [ 1 ] ?? TRAITS_BRACKET_PATTERN . exec ( property ) ?. [ 1 ] ;
145+ }
146+
143147function getTraitValue ( property : string , context ?: GenericEvaluationContext ) : any {
144148 if ( property . startsWith ( '$.' ) ) {
149+ // Look up $.identity.traits.X and $.identity.traits['X'] paths directly
150+ // to avoid jsonpath-plus mis-parsing special characters (e.g. $, [, ]) in
151+ // trait names that appear inside bracket-notation strings.
152+ const traitName = extractTraitNameFromPath ( property ) ;
153+ if ( traitName !== undefined ) {
154+ return context ?. identity ?. traits ?. [ traitName ] ;
155+ }
145156 const contextValue = getContextValue ( property , context ) ;
146157 if ( contextValue !== undefined && isPrimitive ( contextValue ) ) {
147158 return contextValue ;
@@ -179,14 +190,9 @@ export function getContextValue(jsonPath: string, context?: GenericEvaluationCon
179190 if ( ! context || ! jsonPath ?. startsWith ( '$.' ) ) return undefined ;
180191
181192 try {
182- const normalizedPath = normalizeJsonPath ( jsonPath ) ;
183- const results = jsonpath . query ( context , normalizedPath ) ;
193+ const results = JSONPath ( { path : jsonPath , json : context } ) ;
184194 return results . length > 0 ? results [ 0 ] : undefined ;
185195 } catch ( error ) {
186196 return undefined ;
187197 }
188198}
189-
190- function normalizeJsonPath ( jsonPath : string ) : string {
191- return jsonPath . replace ( / \. ( [ ^ . \[ \] ] + ) $ / , "['$1']" ) ;
192- }
0 commit comments