Skip to main content
Inspiring
Sep 27, 2022 9:15 AM UTC
Question

Update value in the nested json

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?

    11 replies

    durgeshk0003
    Participating Frequently
    Sep 27, 2022 9:18 AM UTC

    Check your result variable value. 

    Inspiring
    Sep 27, 2022 9:21 AM UTC

    In result variable it misses some fields. And the data struct is different.

    The origin json

     

    The result variable

    harshitb6843
    Brainy
    Sep 27, 2022 9:50 AM UTC

    Can you paste the JSON here as code for better debugging?

    Inspiring
    Sep 27, 2022 9:56 AM UTC

    [
        {
            "customkey": "",
            "sectioninstructions": "",
            "sectionparent": "root",
            "questions": [
                {
                    "customkey": "",
                    "rowlabelstyle": "",
                    "rowlabel": "CV Submission",
                    "questionrow": [
                        {
                            "customkey": "",
                            "ismandatory": "",
                            "questiontooltip": "",
                            "questionid": 1,
                            "readonly": "",
                            "questioninstructions": "",
                            "questiontitle": "CV Upload",
                            "responsetype": "recordaction",
                            "responsevalue": "",
                            "responsetext": "",
                            "parentid": 1
                        }
                    ]
                }
            ],
            "sectionlevel": 1,
            "nestedjson": "",
            "sectionstyle": "bold",
            "sectiontype": "card",
            "collapsible": true,
            "sectionname": "Questionnaire",
            "objectid": 1,
            "parentid": 0
        }
    ]
    

    This is the json. I need to update the "responsevalue" for the item in the "questionrow"

    harshitb6843
    Brainy
    Sep 27, 2022 10:10 AM UTC

    What are the values of targetField and value Rule Inputs?

    stefanhelzle0001
    Brainy
    Sep 27, 2022 11:12 AM UTC

    You cannot "change" a value in an expression in Appian. instead, you create a modified copy as the returned value.

    Inspiring
    Sep 27, 2022 12:05 PM UTC

    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. 

    stefanhelzle0001
    Brainy
    Sep 27, 2022 12:09 PM UTC

    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.