3 Replies Latest reply on Jul 1, 2020 12:40 PM by Laurent Matheo

    Extract value from JSON Array

    Waldemar Rucha
      Share This:

      Hello *

       

      In a process I have a text variable “Dynamic Data” with the following content:

      [{"name":"severity","value":"2 - Considerable"},{"name":"urgency","value":"High"}]

       

      Now I need to set a second text variable “severity” with “2 - Considerable” from the “Dynamic Data” field.

       

      Any way to achieve this easy?

        • 1. Re: Extract value from JSON Array
          Laurent Matheo

          Hi

           

          So you mentioned the text is coming from a third party application (yes we cheated, we both work at BMC)

           

          So the way I would do that is not using Json parsing or string parsing but leveraging the Object and Document concept.

          Defining a document schema - Documentation for BMC Helix Platform 20.02 - BMC Documentation

           

          What I want ultimately is to create this object:

          {
            "statusList": [
              {
                "name": "severity",
                "value": "2 - Considerable"
              },
              {
                "name": "urgency",
                "value": "High"
              },
              {
                "name": "holiday",
                "value": "Low"
              }
            ]
          }
          

           

          So I need to create two documents, one that will define the Array, one which will define one status, so:

          {
            "statusList": []
          }
          

           

          2020-06-30_13-24-00.png

          And:

              {
                "name": "",
                "value": ""
              }
          

           

          2020-06-30_13-25-10.png

           

          Then in my process I have as:

          • Input Variable:
            • "input status list" as text (will contain [{"name":"severity","value":"2 - Considerable"},{"name":"urgency","value":"High"},{"name":"holiday","value":"Low"}] later),
            • "input status to look for" as text (will contain the status to look for, so here urgency later),
          • Output Variable:
            • "output status value" as text that will contain the found status value (so here will contain High once we find urgency),
          • Local Variable:
            • "var list status" as Data Type Object with Document Definition "doc list of status" that will contain the statuses as array of statuses,
            • "var status" as Data Type Object with Document Definition "doc status" that will contain one status (we will use it later as variable when we loop to search the right status),

           

          Example for one Object and Document definition:

           

          Then as first block I have a "Create Document" that will take the input status list ("input status list") and put it in the object "var list status" "statusList" property, so kinda like this in pseudo code:

          varListStatus.statusList = JSON.parse(inputStatusList);
          

           

          So in the Input you need to specify:

          • The Document (schema) you want to create, so in our case "doc list of status".
          • What node(s) you want to set, so here "statusList",
          • As node Value you need to select the Text passed to your process containing the list of status (so here "input status list")

          Then in Output you want to tell where you want to save what you just did, so:

          • Name is the variable we want to save the Document instance, so "var list status",
          • Source is the value we want to save, so here it is the Output of our current block,

          2020-06-30_13-35-31.png

           

          Backend will try to automatically parse the value passed and cast it as objects.

           

          Now for the search part, we want to do something like this (pseudo code):

          for (<docStatus> varStatus in <docStatus>[] varListOfStatus.statusList) {
              if (varStatus.name === inputStatusToLookFor) {
                  outputStatusValue = varStatus.value;
                  break;
              }
          }
          

          We loop in the list of statuses (var list status.statusList) and we save each status in the variable var status.

          When we have "var status.name" which is the value we are looking for (input status to look for) then we save the "var status.value" into our output variable "output status value".

           

          So for that we are going to use a "Compute Value" block which allows to set value into a variable.

          Then we are going to activate the "loop" system, which is "Multi Instance Loop":

          • Loop Type: Sequential,
          • Loop Data Input: The array we want to loop on, so here "var list of status.statusList",
          • Input Data Item: The variable that will contain the item extracted from the array, so here we save it in "var status",
          • Completion criteria: By default the loop will stop at the end of the array, but we can give a condition on which it can stop before that. Here we want to stop when the status name is the name we are looking for. As the extracted status object at each loop is saved into the variable "var status", we can tell the condition is "var status.name = input status to look for",

          2020-06-30_13-49-39.png

          Now that we have defined the loop and the completion criteria, we can focus on saving our status value.

          • For that it the Input part which defines the value we want to save.
            • In our case since the extracted status object at each loop is saved into the variable "var status", we want to return "var status.value".
          • Then we want to save this value in our variable "output status value", that is in the Output part:
            • Name is the variable we want to save the value in, so here "output status value",
            • Source is the value we want to save, so in our case it is the same block "Output" value,

          2020-06-30_13-54-14.png

           

          So when your process executes:

           

          You will have something like this:

          1 of 1 people found this helpful
          • 2. Re: Extract value from JSON Array
            Waldemar Rucha

            thx Laurent Matheo

            I tried it much complicated without success. Your solution approach is easy and great.