How to handle master detail update and delete request in JSON

Hi Friends ,

We have the JSON Data coming in this format,

$invoice_data = {
    "CONTACT_ID": "1",
    "INV_SERIAL_NO": "100",
    "NAME": "baby",
    "INV_DATE": "2018-06-27",
    "DUE_DATE": "2018-06-27",
    "CURRENCY": "KD",
    "SUBTOTAL": "143",
    "TAX_TOTAL": "13",
     "shipment_data": [
         {
            "SHIP_SERIAL_NO": "44",
            "MASTER_NO": "55",
            "HOUSE_NO": "88",
            "cost_revenue_items": [
                {
                    "CHARGE_REF": "tt",
                    "CURRENCY": "INR",
                    "QUANTITY": "2",
                    "SELLING_RATE": "45",
                    "EXCHANGE_RATE": "987"                                       
                },{
                    "CHARGE_REF": "ii",
                    "CURRENCY": "INR",
                    "QUANTITY": "2",
                    "SELLING_RATE": "45",
                    "EXCHANGE_RATE": "456"
                },{
                    "CHARGE_REF": "op",
                    "CURRENCY": "INR",
                    "QUANTITY": "2",
                    "SELLING_RATE": "45",
                    "EXCHANGE_RATE": "456"
                }
            ]
        }
    ]
}

How to write DELETE End-point for child data-set “cost_revenue_items”. What I mean to say is how to handle the request to delete one of item from “cost_revenue_items”.

When “cost_revenue_items” will be deleted ,master “invoice_data” will be updated. This update , we can handle by PUT, but how do we know under that PUT, we have to
DELETE one of “cost_revenue_items”. Like how the JSON request to only delete one of “cost_revenue_items” will look like.

Will JSON request to delete “cost_revenue_items” looks like this ,if it want to delete last record :

$invoice_data = {
    "CONTACT_ID": "1",
    "INV_SERIAL_NO": "100",
    "NAME": "baby",
    "INV_DATE": "2018-06-27",
    "DUE_DATE": "2018-06-27",
    "CURRENCY": "KD",
    "SUBTOTAL": "143",
    "TAX_TOTAL": "13",
     "shipment_data": [
         {
            "SHIP_SERIAL_NO": "44",
            "MASTER_NO": "55",
            "HOUSE_NO": "88",
            "cost_revenue_items": [
                {
                    "CHARGE_REF": "tt",
                    "CURRENCY": "INR",
                    "QUANTITY": "2",
                    "SELLING_RATE": "45",
                    "EXCHANGE_RATE": "987"
                },{
                    "CHARGE_REF": "ii",
                    "CURRENCY": "INR",
                    "QUANTITY": "2",
                    "SELLING_RATE": "45",
                    "EXCHANGE_RATE": "456"
                }
            ]
        }
    ]
}

Kindly help me to know , how JSON request will look like ?

Thanks a lot

Not quite clear what you’re asking. Is this JSON what you are going to be getting, and you want to know how to delete the extra record when you get it? If so, it looks like you just need to use the “replace” save strategy for your association instead of “append”. Or are you asking how you should format the JSON in order to facilitate deleting that record? That’s a much harder question to answer, as it depends a lot on how the front-end that generates the JSON works.

Hi Zuluru,

Thanks for the reply…

You are right:-
" Is this JSON what you are going to be getting, and you want to know how to delete the extra record when you get it?"

The logic is,
To update a record(array) when it is already existing in database and to insert a record record(array) when it is not existing in the database and i have achieved these conditions.

I needed to know what if an array is deleted and sent as JSON, how should we compare that and delete in our database. “or” will the clients be sending a separate request to delete such array under a master record. We are not aware of the type of request from the client. Is it better to discuss this request with the clients and then proceed.