How to use JSON object in Workflow
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-16-2024 06:50 AM - edited 02-16-2024 06:56 AM
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2024 01:28 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2024 01:51 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2024 02:08 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2024 09:01 AM - edited 02-19-2024 10:58 PM
Hi @Shubham26
Many thanks for the responses.
I just wrote one simple workflow for testing JSON.
Run Script:
If Condition:
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
Can you please guide me to fix this.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2024 11:47 PM
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