No internet connection
  1. Home
  2. General

Power tip: Inline function to grab JSON Value

By Mark Carpenter @mark.carpenter
    2022-06-03 12:38:17.696Z

    This little gem dramatically reduces the number of JSON extracts required to reference and use a value from a JSON structure without requiring local variable space for each element. I have a number of early automations that are filled with local extracted variables for the sole purpose of moving the value into another JSON structure, database, log file, etc.

    inline %func%:JsonValue changes the game. For example, implementing an omni-channel order confirmation solution, the initial automation for each channel creates a common JSON structure for the called automation that sends the order confirmation via one of several email types (Amazon requires plain text, for other channels we can use our email forwarder with its slick templating language).

    Prior to the inline %func%:JsonValue, the called automation would have to extract each and every element of the JSON simply to rearrange for the target email confirm. With %func%:JsonValue, however, any value passing through from the %Msg_Body% to the target format can be referenced directly like this excerpt:

    {
    "OrderItemsTotal": %func%:JsonValue(%Msg_Body%,"OrderItemsTotal"),
    "SalesTaxTotal": %func%:JsonValue(%Msg_Body%,"SalesTaxTotal"),
    "PaidShippingTotal": %func%:JsonValue(%Msg_Body%,"PaidShippingTotal"),
    "OrderTotal": %func%:JsonValue(%Msg_Body%,"OrderTotal"),
    "LineItems": %func%:JsonValue(%Msg_Body%,"LineItems")
    }

    Just one more tip showing off the brilliant power of TA5!

    • 1 replies
    1. M
      Mark Carpenter @mark.carpenter
        2022-06-07 20:53:12.916Z

        I thought to try using the %func%:Format to convert a decimal from positions to two...it works brilliantly, sort of reminds me of LISP but not in an RPN way.

        "PaidShippingTotal": %func%:Format(%func%:JsonValue(%Msg_Body%,"PaidShippingTotal"),F)

        Within the JSON (using Set Variable) it modified the value from 13.5091 to 13.51