a!localVariables( local!metadataJSON: if( a!isNotNullOrEmpty(ri!metadataJSON), a!fromJson(ri!metadataJSON), null ), local!result: a!forEach( items: local!metadataJSON, expression: a!localVariables( local!section: fv!item, a!forEach( items: local!section.questions, expression: a!localVariables( local!questionrow: fv!item.questionrow, a!forEach( items: local!questionrow, expression: if( fv!item.questiontitle = ri!targetField, updatedictionary(fv!item, { "responsetext": ri!value }), null ) ) ) ) ) ), local!metadataJSON )
I have a nested json. I need to update its field "responestext" if its questiontitle is equal to the parameter. Then return the new json. But the original variable metadataJSON is not updated. Is there any function to update the origin value?
Discussion posts and replies are publicly visible
You cannot "change" a value in an expression in Appian. instead, you create a modified copy as the returned value.
Thanks for your suggestion. But How can I do it? Currently I do it in the expression rule. The json, targetField and value are the parameter. I parse the json as a new object in the rule and I do the change on this new object.
Sure. You create the modified copy in local!result but then define the unmodified value as the output.
In case you need a JSON string as the output, call toJson() to do so.
Got it. Thanks! It is great help. But I still cannot understand why I cannot do the update in the forEach directly. Because it is common in other language to update something in the forEach
Appian uses a functional approach. And there is no functional language which allows you to do that. Check out the docs: https://docs.appian.com/suite/help/22.3/Expressions.html#functions-and-side-effects