Convert HashMap to Object

Json File body

[
 {
  "customer": {
     "id": 12,
      "name": "Sam"
      "info": [T]
   },
  "customer": {
     "id": 13,
      "name": "Same"
      "info": [T]
   },
 }
]

Gatling scenario

val checkCustomer: ChainBuilder = {
  .feed(existingCustomerInJson.circular)
  .exec{ session =>
newSession = session.set("customerWithBodyForCheck", buildBody(session("customer").as[Map[String, Any]]))
newSession
}
.exec(sendRequestForCeckCustomer)

Convert Map to Object

import org.json4s.native.Serialization
import org.json4s.native.Serialization.read

  def buildBody(customer:Map[String, Any]): String ={

    implicit val formats = Serialization.formats(org.json4s.NoTypeHints)

    def mapToObject[A: Manifest](map: Map[String, Any]): A = {
      val jsonString = Serialization.write(map)
      read[A](jsonString)
    }

    val customerObject: T = mapToObject[T](customer)

   BodyForCheckCustomer(
id = customerObject.customer.id,
info = customerObject.customer.info
).toString.mkString // or can use circe 

}

case class BodyForCheckCustomer(

id: Int
info: Any

)

Hi @RBI,

Welcome into the Gatling OpenSource Community!
What is your question? What is the relation with Gatling?
From the small amount of code you provide, it’s only related to json4s that is not part of Gatling.
You should ask your question there.

Cheers!

Hi @sbrevet , that is not a question, I just sheared the option if somebody need to convert data from map into object. That problem can appear when get data from json file with gatling’s feed and after that need to operate with them. and that is related to gatling in direct line :wink:

@RBI,

So maybe you may edit the initial post to include such presentation and a sample with direct Gatling usage. Otherwise we can analyze that as a spam (at least as an ad that is not allowed in this community forum).

For now, the initial post is only a copy paste of random scala code that should have it place on gist (or other platform to share snippet of code).

Cheers!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.