This is how I built the variable in Power Automate.
I build the dispatch_lines objects in a loop and then, once that loops is complete, set a variable, "DispatchOrder" using the code below.
{
"dispatch":
{
"order_id":"%CurrentOrder['id']%",
"comments": "Your order has been received for processing by Zero G",
"dispatch_lines":[
%DispatchLines%
]
}
}
I suppose I could do it without the formatting.
The code does have spaces, so I am going to build it without the spaces. It is more readable with the formatting.
Ultimately, converting it to an object and back to JSON solves another issue. When I am building the dispatch_lines, I'm using the Append line to text action.
This is the line, created in a Set Variable action.
{"sku": "%CurrentLine.sku%", "quantity": %CurrentLine.quantity%},
The final dispatch line ends up having a trailing comma - which I could remove. But, again, converting to an object and back to JSON removes it.
I could have to add another action to remove the trailing comma.
When I convert to object and back to JSON, it ends up like this:
{"dispatch":{"order_id":"or_########","comments":"Comment for the order","dispatch_lines":[{"sku":"LYR-01","quantity":2.0}]}}
However, if I create that as a variable and pass it to the API, it fails with a "Bad Request". If I convert that to an object and back to JSON it works.
If I take the same and post it via Postman.com, it works.