cancel
Showing results for 
Search instead for 
Did you mean: 

Python snippet - output dictionary to output schema

mlamotte
Deputy Chef III
Deputy Chef III

In my python snippet I have the following output schema defined, which is a nested list:

pythonsnippet1.JPG
 
the output of my python script (when just doing return output) also is in the same format
pythonsnippet2.JPG
 
But for the life of my I cannot figure out how to map the array I created in python on the output schema in the return statement.
1 ACCEPTED SOLUTION

gary1
Executive Chef III
Executive Chef III

And I don't know Python 😅

I think it's a case sensitivity issue. Your schema screenshot says "TransportOrders", your output screenshot says "transport_orders", and your script screenshot has "Transportorders" 

Workato tends to automatically update CamelCase to snake_case (very annoying, but it's only when displaying input/output) , so I think all you need to do is update your code to "TransportOrders" and you'll be set. Hopefully that works.

View solution in original post

4 REPLIES 4

gary1
Executive Chef III
Executive Chef III

You don't need to map it in, you just need to create the data to match the schema (exactly) and return it. 

If my output schema is this:

py.png

Then this is how I will return:

def main(input):
  ## option 1
  output = {"object": {"number": input["data"]}}
  return output

  ## option 2
  return {"object": {"number": input["data"]}}

(But in reality I would use Ruby 😝 )

mlamotte
Deputy Chef III
Deputy Chef III

I don't have experience (yet) with Ruby 😄

def main(input):
    # Dictionary to store transport order information based on transport number
    transport_order_info_dict = {}
    data = input['List']
    for row in data:
        # Extract relevant fields
        transport_number = row['TransportNumber']
        order_number = row['OrderNumber']
        order_line = row['OrderLine']
        order_line_item = row['OrderLineItem']
        prim_qty = row['PrimQty']

        # Initialize the transport_number entry if not exists
        if transport_number not in transport_order_info_dict:
            transport_order_info_dict[transport_number] = {}

        # Initialize the order_number entry if not exists
        if order_number not in transport_order_info_dict[transport_number]:
            transport_order_info_dict[transport_number][order_number] = []

        # Append the order line item to the corresponding order number
        transport_order_info_dict[transport_number][order_number].append({
            "OrderLine": order_line,
            "OrderLineItem": order_line_item,
            "PrimQty": prim_qty
        })

    # Convert the organized data into the final output structure
    output = []

    for transport_number, orders in transport_order_info_dict.items():
        orders_list = []
        for order_number, order_lines in orders.items():
            order_lines_list = []
            for line in order_lines:
                order_lines_list.append({
                    "PrimQty": line["PrimQty"],
                    "OrderLineItem": line["OrderLineItem"],
                    "OrderLine": line["OrderLine"]
                })
            orders_list.append({
                "OrderNumber": order_number,
                "OrderLines": order_lines_list
            })
        output.append({
            "TransportOrders": [
                {
                    "Orders": orders_list,
                    "transport_number": transport_number
                }
            ]
        })

    # Map the output fields to match the schema
    mapped_output = {
        "Transportorders": [{"transport_number": transport_number,
                             "Orders": [{"OrderNumber": order_number,
                                         "OrderLines": [{"PrimQty": line["PrimQty"],
                                                         "OrderLineItem": line["OrderLineItem"],
                                                         "OrderLine": line["OrderLine"]}
                                                        for line in order_lines]}
                                        for order_number, order_lines in orders.items()]}
                           for transport_number, orders in transport_order_info_dict.items()]
    return mapped_output

But when I try to use the output in the next step, the array stays empty.
Might it be that the TransportOrders should be an object instead of an array?

gary1
Executive Chef III
Executive Chef III

And I don't know Python 😅

I think it's a case sensitivity issue. Your schema screenshot says "TransportOrders", your output screenshot says "transport_orders", and your script screenshot has "Transportorders" 

Workato tends to automatically update CamelCase to snake_case (very annoying, but it's only when displaying input/output) , so I think all you need to do is update your code to "TransportOrders" and you'll be set. Hopefully that works.

mlamotte
Deputy Chef III
Deputy Chef III

you are absolutely right.
i got it working. thanks!