javaarraysjsonscalasparkpost

Using Java API TransmissionWithRecipientArray object, how can I set an element like a key value array ( Sparkpost )


I'm sending emails using the Java API TransmissionWithRecipientArray object against a template. I'm facing some problems with the substitution data. I have test this data in the template editor but I don't know how to introduce that substitution data using TransmissionWithRecipientArray.

Here is a sample:

(...), "offers": [
     {
       "description": "dddddddddddddddddd.",
       "discount": "ddddddd",
       "image": "ddddddddddddddddddddd",
       "image_announcer": "dddddddddddddddddddddddddddd",
       "alt_title": "dddddddddddddddddddddd",
       "tracking": "dhsdjkhsdjksdh",
       "name": "sdhsdohdsiosd",
       "id": "8480515",
       "announcer_paid": "0",
       "announcer_image": "test",
       "announcer_alt_title": "wdiohdiowdhiowd"
     },
      {
       "description": "dddddddddddddddddd.",
       "discount": "ddddddd",
       "image": "ddddddddddddddddddddd",
       "image_announcer": "dddddddddddddddddddddddddddd",
       "alt_title": "dddddddddddddddddddddd",
       "tracking": "dhsdjkhsdjksdh",
       "name": "sdhsdohdsiosd",
       "id": "8480515",
       "announcer_paid": "0",
       "announcer_image": "test",
       "announcer_alt_title": "wdiohdiowdhiowd"
     }, (...)

In other words the question is: What should we introduce in the method setSubstitutionData() to get this input as substitution data? We have validated the substitution data using the template editor.

transmission.setSubstitutionData(allSubstitutionData.asJava)

Mandatory HTML:

 {{offers[1].description}}

Solution

  • Thank you guys for your answers.

    The issue we had here were with the conversion from a Scala Map type to Gson.

    The result of processing with the Gson library HashMaps created from Scala Maps is different. Includes extra fields and changes the structure of the JSON.

    The solution is this answer for Java users, and for Scala: iterate firstly all Maps converting to Java types like this:

     def toJavaConverter(objectLevelSubs: immutable.Map[String, AnyRef]): java.util.LinkedHashMap[String, Object] = {
    val output = new java.util.LinkedHashMap[java.lang.String, Object]
    objectLevelSubs.foreach {      
      case (k: String, v: List[Predef.Map[String, AnyRef]]) => output.put(k, v.map(toJavaConverter))     
      case (k: String, v: Predef.Map[String, AnyRef]) => output.put(k, toJavaConverter(v))
      case (k: String, v: AnyRef) => output.put(k, v)
    }
    output}
    

    And finally converting each element like this.

     val gson: Gson = new GsonBuilder().setPrettyPrinting().enableComplexMapKeySerialization().create()
    val finalSubstitutionData: util.LinkedHashMap[String, AnyRef] = new util.LinkedHashMap[String, AnyRef]()
    
    javaObjectLevelSubs.forEach{
      case (k: String, v: String) => finalSubstitutionData.put(k, v)
      case (k: String, a) => a match {case l: List[_] => finalSubstitutionData.put(k, l.map(gson.toJsonTree).asJava)}
    }
    

    Thanks @Yepher and @balexandre