Sample code for 30+ languages & platforms
Android™

Merge SP-API JSON Order Arrays

See more Amazon SP-API Examples

Demonstrates how to merge JSON arrays for the case of Amazon SP-API orders.

Chilkat Android™ Downloads

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean success = false;

    // Batch 1 File
    String a1 = "{\"payload\": {\"Orders\": [{\"AmazonOrderId\": \"1\",\"OrderStatus\": \"Unshipped\"},{\"AmazonOrderId\": \"2\",\"OrderStatus\": \"Unshipped\"}]}";

    // Batch 2 File
    String 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;
    CkJsonObject json1 = new CkJsonObject();
    json1.Load(a1);

    CkJsonObject json2 = new CkJsonObject();
    json2.Load(a2);

    // We're going to add the order records from json2 to json1.
    CkJsonArray jarr1 = json1.ArrayOf("payload.Orders");
    CkJsonArray jarr2 = json2.ArrayOf("payload.Orders");

    int i = 0;
    int numOrders = jarr2.get_Size();
    while ((i < numOrders)) {
        CkJsonObject jOrder = jarr2.ObjectAt(i);
        // Assuming non-null return...
        jarr1.AddObjectCopyAt(-1,jOrder);
        i = i + 1;
        }

    // Show the merged JSON.
    json1.put_EmitCompact(false);
    Log.i(TAG, json1.emit());

    // Result:

    // {
    //   "payload": {
    //     "Orders": [
    //       {
    //         "AmazonOrderId": "1",
    //         "OrderStatus": "Unshipped"
    //       },
    //       {
    //         "AmazonOrderId": "2",
    //         "OrderStatus": "Unshipped"
    //       },
    //       {
    //         "AmazonOrderId": "3",
    //         "OrderStatus": "Unshipped"
    //       },
    //       {
    //         "AmazonOrderId": "4",
    //         "OrderStatus": "Unshipped"
    //       }
    //     ]
    //   }
    // }

  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}