Expand my Community achievements bar.

Multivalue array must not contain maps/objects

Avatar

Level 3

Hi Team,

 

Our AEM project test cases are Junit4 based. Recently we added Junit 5 dependency as well in POM.

Line causing below exception (with JUNIT 4,  It used to work perfectly fine)

context.load().json("/empFile.json", "/emp");

empFile.json:

{
"empID": "xx57jmf",
"empType": "regular",
"personalinfo": {
"name": "ABC",
"geo": [
{

"id": "geo_456",
"coordinate": "xf73hj"
},
{
"id": "geo_456",
"coordinate": "xf73hj"
}
],
"education": [
{
"state": "Milan",
"duration":"4"
}
]
}
}

 

 

Almost all test cases are throwing below error:

 org.apache.sling.jcr.contentparser.ParseException: Multivalue array must not contain maps/objects.

 

3 Replies

Avatar

Level 5

I guess the error occurs because in Junit5 the Apache Sling Content Parser (used by AEM's load().json() method) enforces strict rules for JSON-to-JCR mapping. Arrays ([]) in JSON can only contain primitive values (strings/numbers) not nested objects/maps.

 

Maybe you can try mimicing JCR’s XML/JSON export format:

{
  "empID": "xx57jmf",
  "empType": "regular",
  "personalinfo": {
    "name": "ABC",
    "geo": { // Child node
      "jcr:primaryType": "nt:unstructured",
      "items": { // Multi-value property
        "items": ["geo_456", "xf73hj"]
      }
    }
  }
}

  

Avatar

Level 3

Hi @giuseppebag,

 

Thanks for your kind reply.

 

Problem is many such JSONs that we have under resource folder are API response. Not any JCR node json export.

Also, There are around 1000 similar cases.

 

Changing JSON structure for each one might not be possible at this moment followed by test classes fix.

 

Is there any quick workaround for such issue?

Avatar

Community Advisor

Hi @arvindpandey ,

The error occurs because Apache Sling Content Parser (used in context.load().json()) enforces stricter rules in JUnit 5.

Issue Explanation:
In JUnit 4, context.load().json("/empFile.json", "/emp") allowed multi-value arrays to contain objects. However, in JUnit 5, arrays ([]) must only contain primitive values (strings, numbers, booleans).

In your JSON:

 

"geo": [
  {
    "id": "geo_456",
    "coordinate": "xf73hj"
  },
  {
    "id": "geo_456",
    "coordinate": "xf73hj"
  }
]

 

The "geo" array contains objects (maps), which is no longer allowed.

Solution 1: Convert to JCR-Compatible Format:

JCR does not support arrays of objects, so convert the array to a structured node instead of a list. Modify "geo" like this:

 

{
  "empID": "xx57jmf",
  "empType": "regular",
  "personalinfo": {
    "name": "ABC",
    "geo": {
      "jcr:primaryType": "nt:unstructured",
      "geo_1": {
        "id": "geo_456",
        "coordinate": "xf73hj"
      },
      "geo_2": {
        "id": "geo_456",
        "coordinate": "xf73hj"
      }
    }
  }
}

 

Why?

     - This removes the array ([]) and creates child nodes instead, which follows JCR structure. 
     - Now "geo" is a node with sub-nodes (geo_1, geo_2) instead of an invalid object array.

Solution 2:
Convert Objects Inside Array to Multi-Value Properties Instead of storing objects, flatten values into a list:

 

{
  "empID": "xx57jmf",
  "empType": "regular",
  "personalinfo": {
    "name": "ABC",
    "geo": {
      "jcr:primaryType": "nt:unstructured",
      "ids": ["geo_456", "geo_456"],
      "coordinates": ["xf73hj", "xf73hj"]
    }
  }
}

 

Why?
   - JCR allows multi-value properties, but only for primitive types (strings, numbers). 
   - Instead of an object array, "geo" now stores ids and coordinates as separate multi-value lists.

 

Regards,
Amit Vishwakarma