Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

smartvalue - converting a list to a JSON object

Elyashiv Grosser January 30, 2022

Hi,

Is there a way to convert a list of values to JSON?

I have a multi-select custom field multi_field with these options:

  1. ABC 123
  2. ABC 456
  3. DEF 123
  4. DEF 456
  5. GHI 123
  6. GHI 456

In a group of issues, options 2+4+5 were chosen, for instance. I want to clone the issues using an automation rule, but to change the chosen options to be all with the suffix 123. meaning:

2: ABC 456 => ABC 123
4: DEF 456 => DEF 123
5: no change

I thought doing so with this smartvalue:

{
"fields": {
"multi_field": "{{issue.fields.multi_field.replace("456","123")}}"
}
}

 but this phrase returns a string ABC 123, DEF 123, GHI 123
while I need to retrieve a JSON object (according to the documentation) like:
[{"value": "ABC 123"}, {"value": "DEF 123"}, {"value": "GHI 123"}]

I tried using the asJsonStringArrayasJsonObjectArray functions but it didn't work for me. 

Does someone have an idea how to solve this problem, maybe in a different way completely?

Thanks in advance

1 answer

1 accepted

1 vote
Answer accepted
Bill Sheboy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 31, 2022

Hi @Elyashiv Grosser 

Have you tried using the JSON array function first, and then performing the replacement of text on the result, and finally use that to set the field in the advanced edit?

If that does not work, you could also try to construct the JSON string using a list iterator over the source field, replacing the text for each one.

Kind regards,
Bill

Elyashiv Grosser February 1, 2022

Thanks @Bill Sheboy!

I used your first idea and it worked!

I didn't find a way to use iterations in the JSON advanced options

(for instance using {{#issue.comments}}...{{/}})

Like Bill Sheboy likes this
Bill Sheboy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 2, 2022

I am glad to learn that helped!

For the second method, I was suggesting something like this to dynamically build the right-side of the JSON...although I have not tried it:

[ {{#issue.fields.multi_field}} { "value" : \"{{value.replace("456","123")}}\" } {{^last}},{{/}}{{/}} ]

I have successfully done this with other functions, but not with replace(), so the extra quotation marks could be a problem.

Like Elyashiv Grosser likes this

Suggest an answer

Log in or Sign up to answer