I am processing a JSON object of orders. They can have between 1 and n orders to process. Each order has between 1 and n line items to read as well.
Here is a representative sample of the JSON:
{
"orders": [
{
"id": "the order id",
"number": 1,
"created": "2023-06-02T18:13:52Z",
"status": "new",
"order_lines": [
{
"id": "orderline_id01",
"sku": "Item SKU",
"quantity": 3,
},
{
"id": "orderline_id02",
"sku": "Item SKU",
"quantity": 2,
}
],
"currency": "USD",
"net_total": 40.0,
"gross_total": 40.0
}
],
"has_more": false
}
I have this working fine.. a For Each loop with a For Each nested inside of it.
As part of the processing, I have to return a sub-set of the data back to the API I'm using. To do so I have to build the new JSON as I iterate through the above For Each Loop.
The JSON in question looks like:
{
"dispatch": {
"order_id": "the order id",
"comments": "Order submitted and will be processed.",
"dispatch_lines": [
{"sku": "Item SKU", "quantity": 3},
{"sku": "Item SKU", "quantity": 2}
]
}
}
Programmatically, this is simple.
My challenge is where I loop through the order lines, I get the SKU and quantity and am building the dispatch lines. I can't see how to iteratively add to a variable. I can append to text - and that is working but I end up with a blank line at the start of the text. I suspect I can remove that initial blank line and then create my object after the last loop of the order lines loop.
Am I missing a more elegant way to build that new JSON?
I hope I was reasonably clear.
Thanks.