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
(SQL Server) Merge SP-API JSON Order ArraysSee more Amazon SP-API ExamplesDemonstrates how to merge JSON arrays for the case of Amazon SP-API orders.
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls. -- CREATE PROCEDURE ChilkatSample AS BEGIN DECLARE @hr int -- Important: Do not use nvarchar(max). See the warning about using nvarchar(max). DECLARE @sTmp0 nvarchar(4000) -- Batch 1 File DECLARE @a1 nvarchar(4000) SELECT @a1 = '{"payload": {"Orders": [{"AmazonOrderId": "1","OrderStatus": "Unshipped"},{"AmazonOrderId": "2","OrderStatus": "Unshipped"}]}' -- Batch 2 File DECLARE @a2 nvarchar(4000) SELECT @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"}]} DECLARE @success int SELECT @success = 1 DECLARE @json1 int -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json1 OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END EXEC sp_OAMethod @json1, 'Load', @success OUT, @a1 DECLARE @json2 int -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json2 OUT EXEC sp_OAMethod @json2, 'Load', @success OUT, @a2 -- We're going to add the order records from json2 to json1. DECLARE @jarr1 int EXEC sp_OAMethod @json1, 'ArrayOf', @jarr1 OUT, 'payload.Orders' DECLARE @jarr2 int EXEC sp_OAMethod @json2, 'ArrayOf', @jarr2 OUT, 'payload.Orders' DECLARE @i int SELECT @i = 0 DECLARE @numOrders int EXEC sp_OAGetProperty @jarr2, 'Size', @numOrders OUT WHILE (@i < @numOrders) BEGIN DECLARE @jOrder int EXEC sp_OAMethod @jarr2, 'ObjectAt', @jOrder OUT, @i -- Assuming non-null return... EXEC sp_OAMethod @jarr1, 'AddObjectCopyAt', @success OUT, -1, @jOrder SELECT @i = @i + 1 END -- Show the merged JSON. EXEC sp_OASetProperty @json1, 'EmitCompact', 0 EXEC sp_OAMethod @json1, 'Emit', @sTmp0 OUT PRINT @sTmp0 -- Result: -- { -- "payload": { -- "Orders": [ -- { -- "AmazonOrderId": "1", -- "OrderStatus": "Unshipped" -- }, -- { -- "AmazonOrderId": "2", -- "OrderStatus": "Unshipped" -- }, -- { -- "AmazonOrderId": "3", -- "OrderStatus": "Unshipped" -- }, -- { -- "AmazonOrderId": "4", -- "OrderStatus": "Unshipped" -- } -- ] -- } -- } EXEC @hr = sp_OADestroy @json1 EXEC @hr = sp_OADestroy @json2 END GO |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.