@@ -140,10 +140,7 @@ func (r *responseImpl) JsonTemplate(expectedJsonTemplate string, values map[stri
140140
141141 // apply provided values into the template
142142 for k , v := range values {
143- // normalize JSONPath-like keys (convert $.a[0].b to a.0.b)
144- key := strings .TrimPrefix (k , "$." )
145- key = strings .ReplaceAll (key , "[" , "." )
146- key = strings .ReplaceAll (key , "]" , "" )
143+ key := normalizeJSONPath (k )
147144
148145 if ! gjson .Get (expectedJson , key ).Exists () {
149146 assert .Fail (r .t , "Json key does not exist in template: " + k )
@@ -164,9 +161,7 @@ func (r *responseImpl) JsonTemplate(expectedJsonTemplate string, values map[stri
164161 continue
165162 }
166163
167- key := strings .TrimPrefix (k , "$." )
168- key = strings .ReplaceAll (key , "[" , "." )
169- key = strings .ReplaceAll (key , "]" , "" )
164+ key := normalizeJSONPath (k )
170165
171166 if ! gjson .Get (actual , key ).Exists () {
172167 assert .Fail (r .t , "Json key does not exist in template: " + k )
@@ -235,3 +230,14 @@ func (r *responseImpl) Log() Response {
235230 r .response .Body )
236231 return r
237232}
233+
234+ // normalizeJSONPath converts jsonpath syntax to gjson/sjson syntax.
235+ // E.g. convert $.a[0].b to a.0.b and $[0].a to 0.a
236+ // This allows a unified path syntax to the caller, regardless which library is used internally.
237+ func normalizeJSONPath (path string ) string {
238+ path = strings .ReplaceAll (path , "[" , "." )
239+ path = strings .ReplaceAll (path , "]" , "" )
240+ path = strings .TrimPrefix (path , "$." )
241+
242+ return path
243+ }
0 commit comments