
Parsing Box Webhook JSON
Parses the JSON object sent by the Box webhook.
2018 © Questetra, Inc. (MIT License)
2018 © Questetra, Inc. (MIT License)
Configs
- I1. Webhook JSON Object(Data Type:Multiple-lines string) *
- O1. Event happened time(Data Type:Date Time)
- O2. User who happens Event (Data Type:Single-line string)
- O3. Webhook Trigger(Data Type:Single-line string)
- O4. Target File or Folder ID(Data Type:Single-line string)
- O5. Target File or Folder Name(Data Type:Single-line string)
- O6. All Upper Folders ID(Data Type:Multiple-lines string)
- O7. All Upper Folders Name(Data Type:Multiple-lines string)
Script
main();
function main(){
var json;
try {
json = JSON.parse(engine.findDataByNumber(configs.get("Json")));
} catch (e){
throw "I1 is not JSON";
}
if (json == null){
throw "No JSON";
}
var objectName = configs.get("ObjectName");
var objectId = configs.get("ObjectID");
var time = configs.get("Time");
var userName = configs.get("UserName");
var pathId = configs.get("PathinfoID");
var pathName = configs.get("PathinfoName");
var trigger = configs.get("Trigger");
timeToJava(json,time);
UserNm(json,userName);
triggerSet(json,trigger)
objectIN(json,objectName,objectId);
pathinfo(json,pathId,pathName);
}
function objectIN(json,objectName,objectId){
var id = "";
var name = "";
if (json.source == null){
return;
}
if (json.source.item != null){
if (json.source.item.id != null){
id = json.source.item.id;
if (Object.prototype.toString.call(id).slice(8, -1) != "String"){
throw "source.item.id is not string";
}
if (id == ""){
throw "source.item.id is blank string";
}
if (objectId != ""){
engine.setDataByNumber(objectId,id);
}
}
if (json.source.item.name != null){
name = json.source.item.name;
if (Object.prototype.toString.call(name).slice(8, -1) != "String"){
throw "source.item.name is not string";
}
if (name == ""){
throw "source.item.name is blank string";
}
if (objectName != ""){
engine.setDataByNumber(objectName,name);
}
}
}else{
if(json.source.id != null){
id = json.source.id;
if (Object.prototype.toString.call(id).slice(8, -1) != "String"){
throw "source.id is not string";
}
if (id == ""){
throw "source.id is blank string";
}
if (objectId != ""){
engine.setDataByNumber(objectId,id);
}
}
if (json.source.name != null){
name = json.source.name;
if (Object.prototype.toString.call(name).slice(8, -1) != "String"){
throw "source.name is not string";
}
if (name == ""){
throw "source.item.name is blank string";
}
if (objectName != ""){
engine.setDataByNumber(objectName,name);
}
}
}
}
function UserNm(json,userName){
var name = "";
if (json.created_by == null){
return;
}
if(json.created_by.name == null){
return;
}
name = json.created_by.name;
if (typeof name != "string"){
throw "created_by.name is not string";
}
if (name == ""){
throw "created_by.name is blank string";
}
if (userName != ""){
engine.setDataByNumber(userName,name);
}
}
function triggerSet(json,trigger){
var triggerName = "";
if (json.trigger == null){
return;
}
triggerName = json.trigger;
if (Object.prototype.toString.call(triggerName).slice(8, -1) != "String"){
throw "trigger is not string";
}
if (triggerName == ""){
throw "trigger is blank string";
}
if (trigger != ""){
engine.setDataByNumber(trigger,triggerName);
}
}
function timeToJava(json,createdTime){
if (json.created_at == null){
return;
}
var createdAt_tmp = json.created_at;
if (createdAt_tmp == ""){
throw "created_at is blank string";
}
var dateFormat = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
try {
var datetime = dateFormat.parse(createdAt_tmp);
} catch(e) {
throw "created_at must be 'YYYY-MM-DDThh:mm:ss-07:00'";
}
if (createdTime != ""){
engine.setDataByNumber(createdTime,new java.sql.Timestamp(datetime.getTime()));
}
}
function pathinfo(json,pathId,pathName){
if (json.source.path_collection == null){
return;
}
if (json.source.path_collection.entries == null){
return;
}
var roots = json.source.path_collection.entries;
if(!Array.isArray(roots)){
throw "source.path_collection.entries must be array";
}
if(roots.length == 0){
throw "source.path_collection.entries is empty";
}
var foldersName = "";
var foldersID = "";
for (var i = 0; i < roots.length;i++){
if (Object.prototype.toString.call(roots[i]).slice(8, -1) != "Object"){
throw "Only Javascript Object can be in source.path_collection.entries";
}
if (roots[i].name == null){
throw "Javascript Object without name property is in source.path_collection.entries";
}
if (Object.prototype.toString.call(roots[i].name).slice(8, -1) != "String"){
throw "Javascript Object with no-string name property is in source.path_collection.entries";
}
foldersName += roots[i].name + "\n";
if (roots[i].id == null){
throw "Javascript Object without id property is in source.path_collection.entries";
}
if (Object.prototype.toString.call(roots[i].id).slice(8, -1) != "String"){
throw "Javascript Object with no-string id property is in source.path_collection.entries";
}
foldersID += roots[i].id + "\n";
}
if (pathId != ""){
engine.setDataByNumber(pathId,foldersID.trim());
}
if (pathName != ""){
engine.setDataByNumber(pathName,foldersName.trim());
}
}
Download
Capture

Pingback: Monitoring Uploads to Folders Using Box Webhook – Questetra Support