Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Delphi DLL) Merge SP-API JSON Order ArraysSee more Amazon SP-API ExamplesDemonstrates how to merge JSON arrays for the case of Amazon SP-API orders.
uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, JsonArray, JsonObject; ... procedure TForm1.Button1Click(Sender: TObject); var a1: PWideChar; a2: PWideChar; success: Boolean; json1: HCkJsonObject; json2: HCkJsonObject; jarr1: HCkJsonArray; jarr2: HCkJsonArray; i: Integer; numOrders: Integer; jOrder: HCkJsonObject; begin // Batch 1 File a1 := '{"payload": {"Orders": [{"AmazonOrderId": "1","OrderStatus": "Unshipped"},{"AmazonOrderId": "2","OrderStatus": "Unshipped"}]}'; // Batch 2 File a2 := '{"payload": {"Orders": [{"AmazonOrderId": "3","OrderStatus": "Unshipped"},{"AmazonOrderId": "4","OrderStatus": "Unshipped"}]}'; // Required Merged File // {"payload": {"Orders": [{"AmazonOrderId": "1","OrderStatus": "Unshipped"},{"AmazonOrderId": "2","OrderStatus": "Unshipped"},{"AmazonOrderId": "3","OrderStatus": "Unshipped"},{"AmazonOrderId": "4","OrderStatus": "Unshipped"}]} success := True; json1 := CkJsonObject_Create(); CkJsonObject_Load(json1,a1); json2 := CkJsonObject_Create(); CkJsonObject_Load(json2,a2); // We're going to add the order records from json2 to json1. jarr1 := CkJsonObject_ArrayOf(json1,'payload.Orders'); jarr2 := CkJsonObject_ArrayOf(json2,'payload.Orders'); i := 0; numOrders := CkJsonArray_getSize(jarr2); while (i < numOrders) do begin jOrder := CkJsonArray_ObjectAt(jarr2,i); // Assuming non-null return... CkJsonArray_AddObjectCopyAt(jarr1,-1,jOrder); i := i + 1; end; // Show the merged JSON. CkJsonObject_putEmitCompact(json1,False); Memo1.Lines.Add(CkJsonObject__emit(json1)); // Result: // { // "payload": { // "Orders": [ // { // "AmazonOrderId": "1", // "OrderStatus": "Unshipped" // }, // { // "AmazonOrderId": "2", // "OrderStatus": "Unshipped" // }, // { // "AmazonOrderId": "3", // "OrderStatus": "Unshipped" // }, // { // "AmazonOrderId": "4", // "OrderStatus": "Unshipped" // } // ] // } // } CkJsonObject_Dispose(json1); CkJsonObject_Dispose(json2); end; |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.