実現したいこと
WixのwebhookをGASで受け取り、受け取ったデータの処理をしたい
前提
wixという店舗運営サイトで、お客様の予約が入るとwebhookが作動してGASのプログラムが実行される仕様を実現させたいです。
実験的に、自分で予約を入れてみると、webhookでGASに送られてきたデータの形式が、JSONではなく変わった形式だと気づきました。この形式のデータから自分の欲しい情報(たとえばstart_date)を抜き出すにはどうすればいいでしょうか?
該当のソースコード
以下のコードでwebhookを受け取り、スプレッドシートに内容を記録しました。
1function doPost(e) { 2 if (e == null || e.postData == null || e.postData.contents == null) return; 3 4 var obj = JSON.parse(e.postData.contents); 5 var now = new Date(); 6 var array = []; 7 array.push(now, obj); 8 9 var ss = SpreadsheetApp.getActive(); 10 var sheet = ss.getSheets()[0]; 11 12 sheet.appendRow(array); 13}
スプレッドシートには以下のように表示されます。見たことのない形式なので、どうやって内容を取り出せばいいかわかりません。
{ data={ bookings_page_url=https://xxxxxxxxxxxxxx.wixsite.com/my-site-1/booking-calendar/サービス名, online_conference_enabled=false, staff_member_name=山田, approval_service=false, booking_contact_first_name=鈴木, order_id=xxxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx, contact={ emails=[Ljava.lang.Object;@xxxxxxx, name={last=鈴木}, address={ subdivisions=[Ljava.lang.Object;@xxxxxx, addressLine=千代田区千代田1-1, formattedAddress=千代田区千代田1-1 }, addresses=[Ljava.lang.Object;@xxxxxxxx, phone=000000000000, email=xxxxxxx@xxxxxx.com, phones=[Ljava.lang.Object;@xxxxxxxx, locale=ja }, booking_contact_phone=000000000000, notify_participants=true, booking_id=xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx, ics={ downloadUrl=https://www.wix.com/bookings/automations/v2/ics/xxxxx-xxxxx-xxxx-xxxx-xxxxxxxxxx, fileName=session.ics }, custom_form_fields=[Ljava.lang.Object;@xxxxxxxx, service_type=APPOINTMENT, service_name=サービス名, business_name=店舗名, booking_creation_date=2024-06-30T16:51:59.326+09:00, end_date=2024-06-30T20:00:00.000+09:00, context={ activationId=xxxxxxxx-xxxxxxxx-xxxx-xxxxxxxxxx, metaSiteId=xxxxxxxx-xxxxx-xxxxx-xxxxx-xxxxxxxxxxxxxx }, contact_id=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx, service_id=xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx, location=大阪府, service_url=https://xxxxxxxxxxxxxxx.wixsite.com/my-site-1/service-page/サービス名, time_zone_override=Asia/Tokyo, number_of_participants=1.0, start_date=2024-06-30T19:00:00.000+09:00, _context={ trigger={key=wix_bookings-sessions_booked}, app={id=xxxxxxxxxxxx-xxxx-xxxx-xxxxxxxxxxxxx}, activation={id=xxxxxxx-xxxx-xxxxx-xxxx-xxxxxxxxxxx}, action={id=xxxxxxx-xxxxx-xxxx-xxxxx-xxxxxxxxxxxxxx}, configuration={id=xxxxxxxxxxxx-xxxx-xxxxx-xxxx-xxxxxxxxxxxxxx} } } }
試したこと
上記の、webhookで得られた内容を別のスプレッドシートに転記し、それをJSONのように処理しようとしましたが、
1function myFunction() { 2 var ss = SpreadsheetApp.getActive(); 3 var sheet = ss.getSheets()[0]; 4 var range = sheet.getRange("A1"); 5 var value = range.getValue(); 6 value = JSON.parse(value); 7 console.log(value.data.start_date); 8}
このようなエラーメッセージが出ます
SyntaxError: Expected property name or '}' in JSON at position 1
補足情報(FW/ツールのバージョンなど)
wixのサイトにあったペイロードの説明は以下の通りです。
pricing_plan 販売プラン remaining_amount_due.value 残額 remaining_amount_due.currency 通貨 booking_creation_date 予約作成日: staff_member_name スタッフ名 online_conference_enabled ビデオ会議が有効にされた order_id 受注 ID business_name ビジネス名 booking_contact_phone Booking contact phone start_date 開始日 location 場所 bookings_page_url 予約ページの URL custom_policy_description カスタムポリシーの説明文 service_image_url サービス画像の URL price.value 金額 price.currency 通貨 custom_price カスタム料金 service_url サービスの URL notify_participants 参加者に通知 staff_member_message スタッフのメッセージ approval_service サービスの承認 service_name サービスタイトル booking_id 予約 ID custom_form_fields カスタムフォーム項目 end_date 終了日 booking_contact_last_name Booking contact last name service_type サービスの種類 currency 通貨 number_of_participants 予約者の人数 contact_id 連絡先 ID service_id サービス ID business_phone 電話番号 online_conference_url ビデオ会議のURL ics.fileName ファイル名 ics.downloadUrl ダウンロード URL online_conference_password ビデオ会議のパスワード business_email ビジネス用メールアドレス online_conference_description ビデオ会議の説明文 booking_contact_first_name Booking contact first name contact.name.last Contact last name contact.name.first Contact first name contact.email Contact email contact.locale Contact locale contact.company Contact company contact.birthdate Contact birthdate contact.address.city Contact city contact.address.addressLine Contact address line 1 contact.address.formattedAddress Contact full address contact.address.country Contact country contact.address.postalCode Contact postal code contact.address.addressLine2 Contact address line 2 contact.address.subdivision Contact address subdivision contact.jobTitle Contact job title contact.phone Contact phone
追記
wix側で、ペイロードの構造をシンプルにしました。しかし、やはりなぜかJSONの要素を取得することができません。
1function doPost(e) { 2 try{ 3 if (e == null || e.postData == null || e.postData.contents == null) return; 4 5 var params = JSON.stringify(e.postData.contents); 6 console.log(params); 7 8 // JSON文字列をオブジェクトに変換 9 let jsonObject; 10 try { 11 jsonObject = JSON.parse(params); 12 console.log('JSON parsed successfully:', jsonObject); 13 } catch (e) { 14 console.error('Failed to parse JSON:', e); 15 } 16 17 // jsonObject自体を確認 18 console.log('jsonObject:', jsonObject); 19 20 // dataプロパティが存在するか確認 21 if (jsonObject && jsonObject.data) { 22 // staff_member_nameを取得 23 const staffMemberName = jsonObject.data.staff_member_name; 24 console.log('Staff Member Name:', staffMemberName); 25 } else { 26 console.error('data property is undefined'); 27 } 28 29 return ContentService.createTextOutput("Received and processed webhook data."); 30 }catch(error){ 31 Logger.log("Error: " + error.message); 32 return ContentService.createTextOutput("Error processing webhook data."); 33 } 34}
ログは以下の通りです。
"{\"data\":{\"staff_member_name\":\"山田 \"}}" JSON parsed successfully: {"data":{"staff_member_name":"山田 "}} jsonObject: {"data":{"staff_member_name":"山田 "}} data property is undefined
さらにやってみたこと
山田の右に謎の空白があったので、削除してみましたが、それでもやはりdataプロパティは見つからないようです。
function doPost(e) { try{ if (e == null || e.postData == null || e.postData.contents == null) return; var params = JSON.stringify(e.postData.contents); params = params.replace( " ", "" ); console.log(params); params = params.substring(1); // JSON文字列をオブジェクトに変換 let jsonObject; try { jsonObject = JSON.parse(params); console.log('JSON parsed successfully:', jsonObject); } catch (e) { console.error('Failed to parse JSON:', e); } // jsonObject自体を確認 console.log('jsonObject:', jsonObject); // dataプロパティが存在するか確認 if (jsonObject && jsonObject.data) { // staff_member_nameを取得 const staffMemberName = jsonObject.data.staff_member_name; console.log('Staff Member Name:', staffMemberName); // 出力: サニ } else { console.error('data property is undefined'); } return ContentService.createTextOutput("Received and processed webhook data."); }catch(error){ Logger.log("Error: " + error.message); return ContentService.createTextOutput("Error processing webhook data."); } }

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。