Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to use JSON object in Workflow

Aruna Sree Yela
Tera Guru

Hi,

 

There is a Record Producer, after submitting it a record will be created in Case table and there is a field called "JSON Output" which stores data of submitted RP

 

JSON body looks as below

{
    “field1”:{
        “original_value”:””,
        “changed_value:””,
        “field_display_value:””
    },

“field2”:{
        “original_value”:””,
        “changed_value:””,
        “field_display_value:””
    },
    …
}

 

Now, I wanted to use the data stored in this "JSON Output" field on Workflow. The condition looks like below

 

ArunaSreeYela_0-1708092135472.png

 

Now I wanted to know how to do below 2 things

 

1. Get JSON data from "JSON Output" field into JSON Object[Action in flow]

2.Here JSON object stores 2fields, so field1 should considered in first if block and I need to write field1!=empty, same for 2nd field.

 

How can I achieve this.

 

Guidance is appreciated.

 

Thanks

 

11 REPLIES 11

Shubham26
Tera Guru

Hi Aruna,

 

May I know in your JSON object, there is only 2 elements like field 1 and field2 or it can be more?

I am asking this question because WF action will depend on this. I believe, there will some definite element in your JSON Object. Please confirm.

 

Thank You.

 

Regards,

Shubham Gupta

Hi @Shubham26 ,

 

The fields are dynamic some times it may be 2 or 5 or 10, max is 16.

The condition behind this is, there are total 16 variables.. out of 16, the JSON stores only the fields that are having values[means not null].

 

Ex:

Field1: abc

Field2: null

Field3: xyz

.

.

.

Field16: null

 

Here, JSON stores only Field1 and Field3

 

Thanks

Hi Aruna,

 

Thank You for your response.

 

You have to use Run script activity and assigned WF scratchpad variable (workflow.scratchpad.jsonObject = JSON.parse(JSON Body);)

Then you can access scratchpad variable in each of the "If condition" activity to check is not null condition as below:

 

if (workflow.scratchpad.jsonObject.field1 != null) {

 return true;

} else {

return false;

}

 

Please mark if this is helpful for you.

 

Thank You.

 

Regards,

Shubham Gupta

 

Hi @Shubham26 

 

Many thanks for the responses.

 

I just wrote one simple workflow for testing JSON.

 

ArunaSreeYela_1-1708361861872.png

 

Run Script:

ArunaSreeYela_2-1708362003728.png

 

If Condition:

ArunaSreeYela_3-1708362050845.png

 

However, the if block was throwing an error. After investigation I understood that, the output of the run script was empty. Please have a look over the below snips

ArunaSreeYela_0-1708412223012.png

 

 

ArunaSreeYela_0-1708361772459.png

 

Can you please guide me to fix this.

 

Thanks

Hi @Aruna Sree Yela 

 

My apologies, you have to return "yes" or "no" from if condition activity as below:

 

if (workflow.scratchpad.jsonObject.field1 != null) {

 return "yes";

} else {

return "no";

}

 

One more thing, I hope u_json_output field is string type field in table, and if null is not working, try to use as below:

 

if (JSUtil.notNil(workflow.scratchpad.jsonObject.field1) {

  return "yes"

} else {

return "no"

}

 

I hope above solution will work for you.

 

Thank You.

 

Regards,

Shubham Gupta