Webhook: How to parse json

Created on 11 Nov 2020  路  6Comments  路  Source: adnanh/webhook

Hi,

I'm a noob and having trouble passing some parts of my payload as arguments.

My hooks.json looks like this:

[
  {  
    "id": "test",
    "execute-command": "/opt/scripts/hook.sh",
     "command-working-directory": "/opt/scripts",
    "pass-arguments-to-command":
    [
      {
        "source": "payload",
        "name": "total_tax"
      },
      {
        "source": "payload",
        "name": "date_created"
      }
    ]

  }
]


It works for the examples above. But if I try other data such as "First_name" or "email" it does not work. There are values like "billing" that seems to have sub-items. The sub-items cannot be read. Is this what is called multi-part form data?

Can someone please write an example hooks.json where i can pass "email" and "First_name" as arguments to my bash script.

Here is my payload as I receive it from WooCommerce (new order from webshop):

{
  "id": 970,
  "parent_id": 0,
  "number": "970",
  "order_key": "wc_order_AuY0YcpTrfKr2",
  "created_via": "checkout",
  "version": "4.6.2",
  "status": "completed",
  "currency": "SEK",
  "date_created": "2020-11-09T20:24:00",
  "date_created_gmt": "2020-11-09T19:24:00",
  "date_modified": "2020-11-11T16:03:41",
  "date_modified_gmt": "2020-11-11T15:03:41",
  "discount_total": "0",
  "discount_tax": "0",
  "shipping_total": "0",
  "shipping_tax": "0",
  "cart_tax": "50",
  "total": "250",
  "total_tax": "50",
  "prices_include_tax": false,
  "customer_id": 1,
  "customer_ip_address": "212.181.170.215",
  "customer_user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36",
  "customer_note": "",
  "billing": {
    "first_name": "John",
    "last_name": "Doe",
    "company": "",
    "address_1": "Test street 4",
    "address_2": "",
    "city": "Stockholm",
    "state": "",
    "postcode": "62151",
    "country": "SE",
    "email": "[email protected]",
    "phone": "+467077xxxxxx"
  },
  "shipping": {
    "first_name": "",
    "last_name": "",
    "company": "",
    "address_1": "",
    "address_2": "",
    "city": "",
    "state": "",
    "postcode": "",
    "country": ""
  },
  "payment_method": "cod",
  "payment_method_title": "Cash on delivery",
  "transaction_id": "",
  "date_paid": "2020-11-11T15:30:35",
  "date_paid_gmt": "2020-11-11T14:30:35",
  "date_completed": "2020-11-11T15:37:13",
  "date_completed_gmt": "2020-11-11T14:37:13",
  "cart_hash": "2020e62ae31e526ceb2e72259b0223f4",
  "meta_data": [
    {
      "id": 6274,
      "key": "is_vat_exempt",
      "value": "no"
    },
    {
      "id": 6455,
      "key": "_shipping_phone",
      "value": ""
    }
  ],
  "line_items": [
    {
      "id": 71,
      "name": "test product",
      "product_id": 927,
      "variation_id": 0,
      "quantity": 1,
      "tax_class": "",
      "subtotal": "200",
      "subtotal_tax": "50",
      "total": "200",
      "total_tax": "50",
      "taxes": [
        {
          "id": 1,
          "total": "50",
          "subtotal": "50"
        }
      ],
      "meta_data": [],
      "sku": "123456",
      "price": 200
    }
  ],
  "tax_lines": [
    {
      "id": 72,
      "rate_code": "SE- TAX-1",
      "rate_id": 1,
      "label": "Tax",
      "compound": false,
      "tax_total": "50",
      "shipping_tax_total": "0",
      "rate_percent": 25,
      "meta_data": []
    }
  ],
  "shipping_lines": [],
  "fee_lines": [],
  "coupon_lines": [],
  "refunds": [],
  "currency_symbol": "kr",
  "_links": {
    "self": [
      {
        "href": "http://commerce.xxxxxxxxxxxxxxxxx.se/wp-json/wc/v3/orders/970"
      }
    ],
    "collection": [
      {
        "href": "http://commerce.xxxxxxxxxxxxxxx.se/wp-json/wc/v3/orders"
      }
    ],
    "customer": [
      {
        "href": "http://commerce.xxxxxxxxxxxxxxxx.se/wp-json/wc/v3/customers/1"
      }
    ]
  }
}
question

Most helpful comment

Thank you. This was really helpfull to know! It works now.

All 6 comments

Try:

[
  {  
    "id": "test",
    "execute-command": "/opt/scripts/hook.sh",
    "command-working-directory": "/opt/scripts",
    "pass-arguments-to-command":
    [
      {
        "source": "payload",
        "name": "total_tax"
      },
      {
        "source": "payload",
        "name": "date_created"
      },
      {
        "source": "payload",
        "name": "billing.email"
      },
      {
        "source": "payload",
        "name": "billing.first_name"
      }
    ]
  }
]

Thank you, this worked! I really appreciate the help.

I have a follow up issue if I may. As I said above, it works for billing.email but line_items.skn does not work. There seems to be a extra [] in the JSON that creates the problem. Can i work around this? Do you have another example for line_items.skn?

I would very mych appreciate.

Yes, use line_items.0.skn to get the first item's skn.

The square brackets ([]) denote an array in JSON. The curly brackets ({}) denote an object (of key-value pairs). Array values are referenced by _index_ (starting with zero). Object values are referenced by _key_.

Thank you. This was really helpfull to know! It works now.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Gareth-3aaa picture Gareth-3aaa  路  4Comments

dcj picture dcj  路  5Comments

alexw22 picture alexw22  路  3Comments

zoenglinghou picture zoenglinghou  路  4Comments

wranitzky picture wranitzky  路  6Comments