@@ -257,31 +257,75 @@ private List<Map<String, String>> parseToMap(List<List<String>> datas, JsonField
257
257
258
258
private List <List <String >> decodeJSON (String data ) {
259
259
ReadContext jsonReadContext = JsonPath .using (jsonConfiguration ).parse (data );
260
- List <List <String >> results = new ArrayList <>(jsonPaths .length );
261
- for (JsonPath path : jsonPaths ) {
262
- List <String > result = jsonReadContext .read (path );
263
- results .add (result );
264
- }
260
+
265
261
if (httpParameter .isJsonFiledMissedReturnNull ()) {
266
- int maxLength = 0 ;
267
- for (List <?> result : results ) {
268
- maxLength = Math .max (maxLength , result .size ());
262
+ // Extract the common parent path from all configured paths
263
+ String commonParentPath = extractCommonParentPath (jsonPaths );
264
+ if (commonParentPath == null ) {
265
+ throw new HttpConnectorException (
266
+ HttpConnectorErrorCode .FIELD_DATA_IS_INCONSISTENT ,
267
+ "Could not find common parent path in JsonPaths. All paths must share a common array parent." );
268
+ }
269
+
270
+ // Get all objects under the common parent path
271
+ List <Map <String , Object >> objects ;
272
+ try {
273
+ objects = jsonReadContext .read (commonParentPath );
274
+ } catch (Exception e ) {
275
+ throw new HttpConnectorException (
276
+ HttpConnectorErrorCode .FIELD_DATA_IS_INCONSISTENT ,
277
+ "Failed to read data from JSON using path "
278
+ + commonParentPath
279
+ + ": "
280
+ + e .getMessage ());
269
281
}
270
- for (int i = 0 ; i < results .size (); i ++) {
271
- List <String > result = results .get (i );
272
- if (result .size () < maxLength ) {
273
- log .warn (
274
- "Field [{}] with size ({}) is less than max size ({}), will be padded with null values. "
275
- + "This may happen when JSON paths return different numbers of elements." ,
276
- jsonPaths [i ].getPath (),
277
- result .size (),
278
- maxLength );
279
- for (int j = result .size (); j < maxLength ; j ++) {
280
- result .add (null );
282
+
283
+ // Create results list for each field
284
+ List <List <String >> results = new ArrayList <>(jsonPaths .length );
285
+ for (int i = 0 ; i < jsonPaths .length ; i ++) {
286
+ results .add (new ArrayList <>(objects .size ()));
287
+ }
288
+
289
+ // For each object in the array
290
+ for (Map <String , Object > obj : objects ) {
291
+ // For each configured field
292
+ for (int i = 0 ; i < jsonPaths .length ; i ++) {
293
+ String fieldPath = jsonPaths [i ].getPath ();
294
+ // Get the relative path from the common parent to the field
295
+ String relativePath = getRelativePath (commonParentPath , fieldPath );
296
+
297
+ try {
298
+ // Create a new context for this object
299
+ ReadContext objContext = JsonPath .using (jsonConfiguration ).parse (obj );
300
+ Object value = objContext .read (relativePath );
301
+ String stringValue = null ;
302
+ if (value != null ) {
303
+ if (value instanceof List ) {
304
+ // If it's a list with one element, take that element
305
+ List <?> list = (List <?>) value ;
306
+ if (!list .isEmpty ()) {
307
+ stringValue = list .get (0 ).toString ();
308
+ }
309
+ } else {
310
+ stringValue = value .toString ();
311
+ }
312
+ }
313
+ results .get (i ).add (stringValue );
314
+ } catch (Exception e ) {
315
+ // If field doesn't exist in this object, add null
316
+ results .get (i ).add (null );
281
317
}
282
318
}
283
319
}
320
+
321
+ return dataFlip (results );
284
322
} else {
323
+ // Original implementation for when isJsonFiledMissedReturnNull is false
324
+ List <List <String >> results = new ArrayList <>(jsonPaths .length );
325
+ for (JsonPath path : jsonPaths ) {
326
+ List <String > result = jsonReadContext .read (path );
327
+ results .add (result );
328
+ }
285
329
for (int i = 1 ; i < results .size (); i ++) {
286
330
List <?> result0 = results .get (0 );
287
331
List <?> result = results .get (i );
@@ -296,8 +340,56 @@ private List<List<String>> decodeJSON(String data) {
296
340
result .size ()));
297
341
}
298
342
}
343
+
344
+ return dataFlip (results );
345
+ }
346
+ }
347
+
348
+ private String extractCommonParentPath (JsonPath [] paths ) {
349
+ if (paths == null || paths .length == 0 ) {
350
+ return null ;
351
+ }
352
+
353
+ // Get all paths as strings
354
+ String [] pathStrings = new String [paths .length ];
355
+ for (int i = 0 ; i < paths .length ; i ++) {
356
+ pathStrings [i ] = paths [i ].getPath ();
357
+ }
358
+
359
+ // Find the array notation position in the first path
360
+ String firstPath = pathStrings [0 ];
361
+ int arrayPos = firstPath .indexOf ("[*]" );
362
+ if (arrayPos == -1 ) {
363
+ throw new HttpConnectorException (
364
+ HttpConnectorErrorCode .FIELD_DATA_IS_INCONSISTENT ,
365
+ "No array notation [*] found in path: " + firstPath );
366
+ }
367
+
368
+ // Get the parent path up to and including [*]
369
+ String parentPath = firstPath .substring (0 , arrayPos + 3 );
370
+
371
+ // Verify all other paths have the same parent
372
+ for (int i = 1 ; i < pathStrings .length ; i ++) {
373
+ if (!pathStrings [i ].startsWith (parentPath )) {
374
+ throw new HttpConnectorException (
375
+ HttpConnectorErrorCode .FIELD_DATA_IS_INCONSISTENT ,
376
+ String .format (
377
+ "Paths have different array parents. Expected '%s' but found path starting with '%s'" ,
378
+ parentPath , pathStrings [i ]));
379
+ }
380
+ }
381
+
382
+ return parentPath ;
383
+ }
384
+
385
+ private String getRelativePath (String parentPath , String fullPath ) {
386
+ // Remove the parent path (including [*]) and keep everything after
387
+ String relativePart = fullPath .substring (parentPath .length ());
388
+ // If the relative part starts with a dot, remove it
389
+ if (relativePart .startsWith ("." )) {
390
+ relativePart = relativePart .substring (1 );
299
391
}
300
- return dataFlip ( results ) ;
392
+ return "$." + relativePart ;
301
393
}
302
394
303
395
private String getPartOfJson (String data ) {
0 commit comments