4 answers
- 210
Hi Rahul, This is because the plugin is returning null. I can't tell which JSON object name you're passing, but the plugin works by expecting specific key names from the response and retrieving values matching those exact keys before saving them to variables or forms. In your case, it's returning null or an empty string because it can't find the key you specified i.e. the whole response.
Here's a better solution: Use the Enhanced JSON API Tool instead, which lets you format the response before storing it in workflow variables or forms.
Enable the "Format Response" option and use this beanshell code. This code converts your API data into a JSON string and stores it in a map with the key "response" - this lets you capture the entire API response in your workflow variable by using "response" as the JSON Object Name.
I have also attached the app for your reference. Hope this helps!!import org.joget.workflow.model.service.*; import java.util.*; import org.joget.commons.util.LogUtil; import org.json.JSONObject; import org.json.JSONArray; JSONObject jsonObject = new JSONObject(data); // Convert the data Hashmap to JSON string String jsonString = jsonObject.toString(); LogUtil.info( "Original data map:----------- " , jsonString.toString()); Map formattedResponse = new HashMap(); formattedResponse.put("response", jsonString.toString()); // Store entire data object under "response" key and use this key to store the full response in wf variable return formattedResponse;Add your comment... - 10-1
Hi Hugo
I am trying to save the JSON response into workflow variable and use this variable in next tool to save JSON data in table (or may be also show in form) using beanshell. my JSON is complex so i need to use beanshell.
I have tried to show the problem using a sample json. the app is attached.
The only way to get the JSON in workflow variable is to write the code in the "Format Response" script box.
Can you advise how i can directly save the JSON into workflow variable.
-----------work around Code Starts--------------
import com.fasterxml.jackson.databind.ObjectMapper;import java.util.LinkedHashMap;// Parse JSONObjectMapper objectMapper = new ObjectMapper();LinkedHashMap<String, Object> orderedJsonMap = objectMapper.readValue(data.toString(), LinkedHashMap.class);// Convert back to JSON string if neededString orderedJsonString = objectMapper.writeValueAsString(orderedJsonMap);// Save the ordered JSON string into the workflow variablewm.activityVariable(workflowAssignment.getActivityId(), "wfv_json", orderedJsonString);-----------work around Code Ends--------------APP_jsonToWorflow-1-20241219123954.jwa
Add your comment... - 10-1
hi Varnaa
thanks for the idea, it did not strike me to use the formating are to assign the value to variable.
i have used the below code to assign the value to workflow variable. atleaset i am getting the value in next tool to process the joson and insert it into table directly.
import org.joget.workflow.model.service.*;
import org.json.JSONObject;
import org.joget.commons.util.LogUtil;// Save the API response directly into a workflow variable
WorkflowManager wm = (WorkflowManager) pluginManager.getBean("workflowManager");
wm.activityVariable(workflowAssignment.getActivityId(), "product_json", data.toString()); // Save the raw API response into the workflow variable// Log the saved response for debugging
LogUtil.info("Save JSON API Response", "API response saved to workflow variable 'product_json': " + data.toString());Add your comment... - 10-1
thanks Varnaa
i tried the code , but still no value in the Workflow variable.
workflow variable:
INFO 18 Dec 2024 18:53:24 Beanshell Script - jwc_rahul : Constructed API URL product_json: void
Error Below ===================
INFO 18 Dec 2024 18:53:24 org.joget.marketplace.EnhancedJsonTool - jwc_rahul : GET : https://dummyjson.com/products/3
2751
INFO 18 Dec 2024 18:53:24 org.joget.marketplace.EnhancedJsonTool - jwc_rahul : https://dummyjson.com/products/3 returned with status : 200
2752
INFO 18 Dec 2024 18:53:24 org.joget.marketplace.EnhancedJsonTool - jwc_rahul : {"id":3,"title":"Powder Canister","description":"The Powder Canister is a finely milled setting powder designed to set makeup and control shine. With a lightweight and translucent formula, it provides a smooth and matte finish.","category":"beauty","price":14.99,"discountPercentage":18.14,"rating":3.82,"stock":59,"tags":["beauty","face powder"],"brand":"Velvet Touch","sku":"9EN8WLT2","weight":8,"dimensions":{"width":24.16,"height":10.7,"depth":11.07},"warrantyInformation":"2 year warranty","shippingInformation":"Ships in 1-2 business days","availabilityStatus":"In Stock","reviews":[{"rating":5,"comment":"Very happy with my purchase!","date":"2024-05-23T08:56:21.618Z","reviewerName":"Ethan Thompson","reviewerEmail":"ethan.thompson@x.dummyjson.com"},{"rating":4,"comment":"Great value for money!","date":"2024-05-23T08:56:21.618Z","reviewerName":"Levi Hicks","reviewerEmail":"levi.hicks@x.dummyjson.com"},{"rating":5,"comment":"Highly impressed!","date":"2024-05-23T08:56:21.618Z","reviewerName":"Hazel Gardner","reviewerEmail":"hazel.gardner@x.dummyjson.com"}],"returnPolicy":"60 days return policy","minimumOrderQuantity":25,"meta":{"createdAt":"2024-05-23T08:56:21.618Z","updatedAt":"2024-05-23T08:56:21.618Z","barcode":"0516267971277","qrCode":"https://assets.dummyjson.com/public/qr-code.png"},"images":["https://cdn.dummyjson.com/products/images/beauty/Powder%20Canister/1.png"],"thumbnail":"https://cdn.dummyjson.com/products/images/beauty/Powder%20Canister/thumbnail.png"}
2753
INFO 18 Dec 2024 18:53:24 Original data map:----------- - jwc_rahul : {"images":["https://cdn.dummyjson.com/products/images/beauty/Powder%20Canister/1.png"],"thumbnail":"https://cdn.dummyjson.com/products/images/beauty/Powder%20Canister/thumbnail.png","minimumOrderQuantity":"25","rating":"3.82","returnPolicy":"60 days return policy","description":"The Powder Canister is a finely milled setting powder designed to set makeup and control shine. With a lightweight and translucent formula, it provides a smooth and matte finish.","weight":"8","warrantyInformation":"2 year warranty","title":"Powder Canister","tags":["beauty","face powder"],"discountPercentage":"18.14","reviews":[{"date":"2024-05-23T08:56:21.618Z","rating":"5","reviewerName":"Ethan Thompson","comment":"Very happy with my purchase!","reviewerEmail":"ethan.thompson@x.dummyjson.com"},{"date":"2024-05-23T08:56:21.618Z","rating":"4","reviewerName":"Levi Hicks","comment":"Great value for money!","reviewerEmail":"levi.hicks@x.dummyjson.com"},{"date":"2024-05-23T08:56:21.618Z","rating":"5","reviewerName":"Hazel Gardner","comment":"Highly impressed!","reviewerEmail":"hazel.gardner@x.dummyjson.com"}],"price":"14.99","meta":{"createdAt":"2024-05-23T08:56:21.618Z","qrCode":"https://assets.dummyjson.com/public/qr-code.png","barcode":"0516267971277","updatedAt":"2024-05-23T08:56:21.618Z"},"shippingInformation":"Ships in 1-2 business days","id":"3","availabilityStatus":"In Stock","category":"beauty","stock":"59","sku":"9EN8WLT2","brand":"Velvet Touch","dimensions":{"width":"24.16","depth":"11.07","height":"10.7"}}
2754
INFO 18 Dec 2024 18:53:24 Beanshell Script - jwc_rahul : Constructed API URL product_json: void
2755
INFO 18 Dec 2024 18:53:24 Beanshell Script - jwc_rahul : Constructed API URLuserInputId : {"images":["https://cdn.dummyjson.com/products/images/beauty/Powder%20Canister/1.png"],"thumbnail":"https://cdn.dummyjson.com/products/images/beauty/Powder%20Canister/thumbnail.png","minimumOrderQuantity":"25","rating":"3.82","returnPolicy":"60 days return policy","description":"The Powder Canister is a finely milled setting powder designed to set makeup and control shine. With a lightweight and translucent formula, it provides a smooth and matte finish.","weight":"8","warrantyInformation":"2 year warranty","title":"Powder Canister","tags":["beauty","face powder"],"discountPercentage":"18.14","reviews":[{"date":"2024-05-23T08:56:21.618Z","rating":"5","reviewerName":"Ethan Thompson","comment":"Very happy with my purchase!","reviewerEmail":"ethan.thompson@x.dummyjson.com"},{"date":"2024-05-23T08:56:21.618Z","rating":"4","reviewerName":"Levi Hicks","comment":"Great value for money!","reviewerEmail":"levi.hicks@x.dummyjson.com"},{"date":"2024-05-23T08:56:21.618Z","rating":"5","reviewerName":"Hazel Gardner","comment":"Highly impressed!","reviewerEmail":"hazel.gardner@x.dummyjson.com"}],"price":"14.99","meta":{"createdAt":"2024-05-23T08:56:21.618Z","qrCode":"https://assets.dummyjson.com/public/qr-code.png","barcode":"0516267971277","updatedAt":"2024-05-23T08:56:21.618Z"},"shippingInformation":"Ships in 1-2 business days","id":"3","availabilityStatus":"In Stock","category":"beauty","stock":"59","sku":"9EN8WLT2","brand":"Velvet Touch","dimensions":{"width":"24.16","depth":"11.07","height":"10.7"}}
2756
INFO 18 Dec 2024 18:53:24 Beanshell Script - jwc_rahul : Constructed API URL: void
2757
INFO 18 Dec 2024 18:53:24 Beanshell Script - jwc_rahul : JSON processed successfully.
2758
INFO 18 Dec 2024 18:53:25 org.joget.workflow.shark.WorkflowAssignmentManager - jwc_rahul : [processId=2437_jsonapiWFVTest_process1, processDefId=jsonapiWFVTest#5#process1, participantId=participant1, next user=[admin]]Add your comment...






Hi
In Process Builder, I am using JSON API tool to get a JSON and trying to save the JSON data into the workflow variable.
but the problem is that variable is always "void" (blank). I want the full json in the variable. i want to use this variable in next tool to save the data in forma and table using bean shell.
can some one advise why it is always blank.
4
INFO 18 Dec 2024 15:46:46 org.joget.marketplace.EnhancedJsonTool - jwc_rahul : GET : https://dummyjson.com/products/3
2695
INFO 18 Dec 2024 15:46:46 org.joget.marketplace.EnhancedJsonTool - jwc_rahul : https://dummyjson.com/products/3 returned with status : 200
2696
INFO 18 Dec 2024 15:46:46 org.joget.marketplace.EnhancedJsonTool - jwc_rahul : {"id":3,"title":"Powder Canister","description":"The Powder Canister is a finely milled setting powder designed to set makeup and control shine. With a lightweight and translucent formula, it provides a smooth and matte finish.","category":"beauty","price":14.99,"discountPercentage":18.14,"rating":3.82,"stock":59,"tags":["beauty","face powder"],"brand":"Velvet Touch","sku":"9EN8WLT2","weight":8,"dimensions":{"width":24.16,"height":10.7,"depth":11.07},"warrantyInformation":"2 year warranty","shippingInformation":"Ships in 1-2 business days","availabilityStatus":"In Stock","reviews":[{"rating":5,"comment":"Very happy with my purchase!","date":"2024-05-23T08:56:21.618Z","reviewerName":"Ethan Thompson","reviewerEmail":"ethan.thompson@x.dummyjson.com"},{"rating":4,"comment":"Great value for money!","date":"2024-05-23T08:56:21.618Z","reviewerName":"Levi Hicks","reviewerEmail":"levi.hicks@x.dummyjson.com"},{"rating":5,"comment":"Highly impressed!","date":"2024-05-23T08:56:21.618Z","reviewerName":"Hazel Gardner","reviewerEmail":"hazel.gardner@x.dummyjson.com"}],"returnPolicy":"60 days return policy","minimumOrderQuantity":25,"meta":{"createdAt":"2024-05-23T08:56:21.618Z","updatedAt":"2024-05-23T08:56:21.618Z","barcode":"0516267971277","qrCode":"https://assets.dummyjson.com/public/qr-code.png"},"images":["https://cdn.dummyjson.com/products/images/beauty/Powder%20Canister/1.png"],"thumbnail":"https://cdn.dummyjson.com/products/images/beauty/Powder%20Canister/thumbnail.png"}
2697
INFO 18 Dec 2024 15:46:46 Beanshell Script - jwc_rahul : Constructed API URL product_json: void
2698
INFO 18 Dec 2024 15:46:46 Beanshell Script - jwc_rahul : Constructed API URLuserInputId :
2699
INFO 18 Dec 2024 15:46:46 org.joget.workflow.shark.WorkflowAssignmentManager - jwc_rahul : [processId=2431_jsonapiWFVTest_process1, processDefId=jsonapiWFVTest#5#process1, participantId=participant1, next user=[admin]]