Server Action Unpacking Nested API JSON results

After reading this discussion How to properly identify JSON API field properties?, I have some questions on how to unpack the nested API JSON results on the server side.

As discussed in Conditional Logic with Validate Data, I have an API where I retrieve data from and then insert it into the mysql database.

The next step in my linking to the API endpoints has several nested JSON parts. For me to be able to insert the data into my mysql database, I somehow need to unpack the data for retrieval and use in my insert statement.

For example, part of the nested JSON has the following:

    "transaction": {
      "transId": "12345",
      "order": {
        "invoiceNumber": "INV00001",
        "description": "some description",
        "purchaseOrderNumber": "PO000001"
      },
      "lineItems": {
        "lineItem": [
          {
            "itemId": "ITEM00001",
            "name": "name of item sold",
            "description": "Description of item sold",
            "quantity": "1",
            "unitPrice": "6.95",
            "taxable": "true"
          },
          {
            "itemId": "ITEM00002",
            "name": "name of item sold",
            "description": "Description of item sold",
            "quantity": "1",
            "unitPrice": "160.99",
            "taxable": "true"
          }
        ]
      }
}

What I would need to do is be able to insert the results into at multiple different tables, for example, one for general transaction info and the other for items purchased. As shown above, one transaction could have multiple items purchased.

Thanks in advance. :slight_smile:

Community Page
Last updated: