
BoxのWebhookのJSONを解析
BoxのWebhookにより送られてきたJSONオブジェクトを解析します。
2018 © Questetra, Inc. (MIT License)
https://support.questetra.com/ja/addons/box-webhook-json-parse/
Configs
- I1.Webhookで送られてきたJSONオブジェクト(指定:複数行文字型データ) *
- O1.イベント発生時刻(指定:日時型データ)
- O2.イベントを起こしたユーザ(指定:単一行文字型データ)
- O3.トリガー(指定:単一行文字型データ)
- O4.対象となるファイル・フォルダのID(指定:単一行文字型データ)
- O5.対象となるファイル・フォルダの名前(指定:単一行文字型データ)
- O6.全ての上位フォルダのID(指定:複数行文字型データ)
- O7.全ての上位フォルダの名前(指定:複数行文字型データ)
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
