Json API response manipulation PHP

127 views Asked by At

I am trying to modify a json response from an API (MYOB in this case) so I can update a record within MYOB. This requires me to grab the full json response modify what I need and put the entire json response back to the api.

What is the best way to manipulte the json response as per below.

{
"UID": "XX-XXXXX",
"Number": "00002024",
"Date": "2020-10-01T00:00:00",
"SupplierInvoiceNumber": "0001234",
"Supplier": {
    "UID": "XX-XXXXX",
    "Name": "Some Supplier",
    "DisplayID": "*None",
    "URI": "https://ar2.api.myob.com/accountright/xxx-xxx/Contact/Supplier/xxx-xxx"
},
"ShipToAddress": "Some Supplier\r\n",
"Terms": {
    "PaymentIsDue": "InAGivenNumberOfDays",
    "DiscountDate": 0,
    "BalanceDueDate": 30,
    "DiscountForEarlyPayment": 0.0,
    "MonthlyChargeForLatePayment": 0.0,
    "DiscountExpiryDate": "2020-10-01T00:00:00",
    "Discount": 0.00,
    "DiscountForeign": null,
    "DueDate": "2020-10-31T00:00:00"
},
"IsTaxInclusive": true,
"IsReportable": false,
"Lines": [
    {
        "RowID": 215233,
        "Type": "Transaction",
        "Description": "DUMMY",
        "UnitOfMeasure": null,
        "UnitCount": null,
        "UnitPrice": null,
        "UnitPriceForeign": null,
        "DiscountPercent": null,
        "Total": 1.500000,
        "TotalForeign": null,
        "Account": {
            "UID": "xxx-xxx",
            "Name": "Office Expenses",
            "DisplayID": "1-2345",
            "URI": "https://ar2.api.myob.com/accountright/xxx-xxx/Contact/Supplier/xxx-xxx"
        },
        "Job": null,
        "TaxCode": {
            "UID": "xxx-xxx",
            "Code": "GST",
            "URI": "https://ar2.api.myob.com/accountright/xxx-xxx/Contact/Supplier/xxx-xxx"
        },
        "RowVersion": "6983429356563464192"
    }
],
"Subtotal": 1.000000,
"SubtotalForeign": null,
"Freight": 0.000000,
"FreightForeign": null,
"FreightTaxCode": {
    "UID": "xxx-xxx",
    "Code": "FRE",
    "URI": "https://ar2.api.myob.com/accountright/xxx-xxx/Contact/Supplier/xxx-xxx",
},
"TotalTax": 0.090000,
"TotalTaxForeign": null,
"TotalAmount": 1.500000,
"TotalAmountForeign": null,
"Category": null,
"Comment": "EZESCAN",
"ShippingMethod": null,
"PromisedDate": null,
"JournalMemo": "Purchase; Some Supplier",
"BillDeliveryStatus": "Nothing",
"AppliedToDate": 0.000000,
"AppliedToDateForeign": null,
"BalanceDueAmount": 1.000000,
"BalanceDueAmountForeign": null,
"Status": "Open",
"LastPaymentDate": null,
"Order": null,
"ForeignCurrency": null,
"CurrencyExchangeRate": null,
"URI": "https://ar2.api.myob.com/accountright/xxx-xxx/Contact/Supplier/xxx-xxx",
"RowVersion": "6262853416184184832"
}

I need to modify Lines[0]->TotalForeign to add a value "$1.0000" etc.. and the part I am really not sure about is "ForeignCurrency": null

I need to modify it as per below

"ForeignCurrency": {
    "UID": "xxx-xxx",
    "Code": "USD",
    "CurrencyName": "US Dollar",
    "CurrencyRate": 0.71428000,
    "URI": "https://ar2.api.myob.com/accountright/xxx-xx/GeneralLedger/Currency/xxx-xxx",
    "RowVersion": "-6496407278109851648"
},

I have tried to split the json using string manipulations then make the change then re enconde the json but this doesn;t seem like the correct way to do this.

Any help would be greatly appreciated.

Thanks

EDIT

Think I may have worked it out.

<?php

$jsonobj = '{ As above reduce for easier reading}';

$arrayData = json_decode($jsonobj, true);

//One way
$replacementData = array('Lines' => array('0' => array('Total' =>'12345678')));
$newArrayData = array_replace_recursive($arrayData, $replacementData);

//Anotherway
$replacementData = [
  'UID'=> "xxx-xxx",
  'Code'=> "USD",
  'CurrencyName'=> "US Dollar",
  'CurrencyRate'=> 0.71428000,
  'URI'=> "https://ar2.api.myob.com/accountright/xxx-xxx/GeneralLedger/Currency/xxx-xxx",
  'RowVersion'=> "-6496407278109851648"
];

//Add the new content data.
$newArrayData['ForeignCurrency'] = $replacementData;

echo "<pre>"; 
echo json_encode($newArrayData, JSON_PRETTY_PRINT);
echo "</pre>";
0

There are 0 answers