Script for JSON Parse Action in Flow Designer

Robert T
Tera Contributor

We built an ACTION in Flow Designer that returns JSON from a REST endpoint that looks like this:

{"version":"21.4.73","treesize":1,"devices":[{"objid":12783,"objid_raw":12783,"status":"Up","status_raw":3,"name":"addc.com","name_raw":"addc.com"}]}

We want to have just the "objid" return as an output from the action, so we added an input step after the REST call using the JSON PARSER:

find_real_file.png

 

The objid string is not selectable as an output (the data pill picker or drag-and-drop both just ignore the attempt to select it).

I created a HI ticket, and was told to make a custom script for grabbing this.  I don't know how to do this.

I added a script step, and then hacked at it a bit, but to no avail.

find_real_file.png

I would like a script which isolates the number of the "objid" value and makes that number selectable in the action step outputs.  Any help or direction will be greatly appreciated.

Thanks!

--Robert

1 ACCEPTED SOLUTION

Dan H
Tera Guru

Hi Robert,

 

I created a custom action for this. Please look over how it is working. You will need to provide the JSON string 

{"version":"21.4.73","treesize":1,"devices":[{"objid":12783,"objid_raw":12783,"status":"Up","status_raw":3,"name":"addc.com","name_raw":"addc.com"}]}

as an input, from whichever data pill is storing it. In my example, I am just passing it in via manual input for testing.

 

Inputs: For you this will be the data pill that contains the JSON response

find_real_file.png

Script step:

Takes the JSON string, parses it into a JSON object.

Assigns outputs.objid to the objid property in the JSON obj.

find_real_file.png

Outputs: Return the objid that we retrieved in the script step to the main flow.

find_real_file.png

Testing action - passing in the JSON string (response)

find_real_file.png

 

Result: objid is 12783

find_real_file.png

 

Hope this helps.

Please mark my answer as Correct/Helpful based on impact

Regards,

Dan H

View solution in original post

3 REPLIES 3

Dan H
Tera Guru

Hi Robert,

 

I created a custom action for this. Please look over how it is working. You will need to provide the JSON string 

{"version":"21.4.73","treesize":1,"devices":[{"objid":12783,"objid_raw":12783,"status":"Up","status_raw":3,"name":"addc.com","name_raw":"addc.com"}]}

as an input, from whichever data pill is storing it. In my example, I am just passing it in via manual input for testing.

 

Inputs: For you this will be the data pill that contains the JSON response

find_real_file.png

Script step:

Takes the JSON string, parses it into a JSON object.

Assigns outputs.objid to the objid property in the JSON obj.

find_real_file.png

Outputs: Return the objid that we retrieved in the script step to the main flow.

find_real_file.png

Testing action - passing in the JSON string (response)

find_real_file.png

 

Result: objid is 12783

find_real_file.png

 

Hope this helps.

Please mark my answer as Correct/Helpful based on impact

Regards,

Dan H

Robert T
Tera Contributor

Worked like a charm.  Thank you Dan!!!!

Robert T
Tera Contributor

Dan,

Should I be so lucky to have you available to continue troubleshooting here.  Some of the response bodies are NULL, at which point we want the action to do something different.  Can you advise how to make this if / else work?

(function execute(inputs, outputs) {
    var jsonString = inputs.jsonString;
      var jsonObj = JSON.parse(jsonString);
      if (jsonObj.devices.length !== 0) {  
          outputs.objid = jsonObj.devices[0].objid.toString(); 
        }         
      else { outputs.objid = null };
    })(inputs, outputs);

--Robert