Nadeem
Nadeem

Reputation: 91

Unable to concatenate a string with another variable

This is my code.

%dw 2.0
output application/java
var plantDesc = payload.somevalue
var str1 = "\"plantDescFull\" = " ++ "\"" ++ plantDesc ++ "\""
---
{
   testValue : str1
}

I need the output to be

{
   testValue : ""plantDescFull" = "someValue""
}

I have tried ways to set str1 but doesn't seem to work. Either I am not able to escape the the quotes or unable to put the value of plantDesc variable into str1

All help is appreciated

Upvotes: 1

Views: 314

Answers (3)

maddestroyer7
maddestroyer7

Reputation: 263

You can't have double quotes inside a string without having them escaped. String interpolation is always a good and clean approach

%dw 2.0
output application/java
var plantDesc = payload.somevalue default ''
---
{
   testValue : "'plantDescFull' = '$(plantDesc)'"
}

If you must have double quotes you would escape them similarly with:

%dw 2.0
output application/json
var plantDesc = payload.somevalue default ''
---
{
   testValue : "\"plantDescFull\" = \"$(plantDesc)\""
}

Upvotes: 0

Nadeem
Nadeem

Reputation: 91

I was able to resolve the problem.

Actual problem was something else. I had to rework my code.

Basically payload.somevalue is an object. So what I was doing was completely wrong. We cannot concatenate a string and an object.

Upvotes: 0

afelisatti
afelisatti

Reputation: 2835

I think it works if you define it with single quotes:

var str1 = '"plantDescFull" = "' ++ plantDesc ++ '"'

Upvotes: 2

Related Questions