Trying to convert a Map from a list in session

I need to form this:

{

“item” : “hardcoded_value”,
“item2” : “hardcoded_value”,
“item3” : “hardcoded_value”,

}

In the exec block I tried:

val theList = session(“itemNames”).as[List[String]] // list with items [“item1”, “ item2”, “ item3”]
val theMap = Map.empty[String,String] // empty map

theList.foreach{ key =>

theMap += key → “hardcoded_value”

}

But getting error at += position.

Also tried:

theList.foreach(key => theMap += key → “hardcoded_value” )

How to insert key and value into a map by iterating over a list? I am new to gatling and scala.

Hi,

Use var declaration to use += operator (e.g):

var theMap = Map.empty[String,String] // empty map

Regards,
Alex.

Or simply:

theList.map(key => (key, “hardcoded_value”)).toMap :slight_smile:

Thanks a lot.
This is working.
I have to pass this map object in a POST call as request body like below after getting it from session.

.body(StringBody("""{${assertPermMap}}""")).asJSON

But it is not working. I am storing it in the session like below.
session.set(“assertPermMap”, (resourcesList.map(item => (item, session(“operation”).as[String])).toMap).mkString(","))

The issue is the format of map object.

The current format is like below.

{ “key1” → value1, “key2” → value2}

But, it will work if the format of map should be like below.
{ “key1” : “value1”, “key2” : “value2”}

please suggest.