Sample code for 30+ languages & platforms
React Native

JSON Copy Objects

See more JSON Examples

Copy objects from one JSON document to another.

Chilkat React Native Downloads

React Native
import { JsonObject } from '@chilkat/react-native'

function chilkatExample() {
  const json1 = new JsonObject();
  json1.updateString('ID1.cn', 'Name');
  json1.updateString('ID1.objectGUID', 'GUID');
  json1.updateString('ID2.cn', 'Name');
  json1.updateString('ID2.objectGUID', 'GUID');

  json1.emitCompact = false;
  console.log(json1.emit());

  // json1 contains:
  // {
  //   "ID1": {
  //     "cn": "Name",
  //     "objectGUID": "GUID"
  //   },
  //   "ID2": {
  //     "cn": "Name",
  //     "objectGUID": "GUID"
  //   }
  // }

  const json2 = new JsonObject();
  json2.updateString('Name1.ID1.cn', 'Name');
  json2.updateString('Name1.ID1.objectGUID', 'GUID');
  json2.updateString('Name1.ID2.cn', 'Name');
  json2.updateString('Name1.ID2.objectGUID', 'GUID');
  json2.updateString('Name2.ID3.cn', 'Name');
  json2.updateString('Name2.ID3.objectGUID', 'GUID');

  json2.emitCompact = false;
  console.log(json2.emit());

  // {
  //   "Name1": {
  //     "ID1": {
  //       "cn": "Name",
  //       "objectGUID": "GUID"
  //     },
  //     "ID2": {
  //       "cn": "Name",
  //       "objectGUID": "GUID"
  //     }
  //   },
  //   "Name2": {
  //     "ID3": {
  //       "cn": "Name",
  //       "objectGUID": "GUID"
  //     }
  //   }
  // }

  // Copy Name1, Name2 into json1

  let i = 0;
  const numMembers = json2.size;
  while (i < numMembers) {
    const jsonObj = json2.objectAt(i);
    json1.appendObjectCopy(json2.nameAt(i), jsonObj);
    i++;
  }

  // Now see what json1 contains:
  console.log(json1.emit());

  // {
  //   "ID1": {
  //     "cn": "Name",
  //     "objectGUID": "GUID"
  //   },
  //   "ID2": {
  //     "cn": "Name",
  //     "objectGUID": "GUID"
  //   },
  //   "Name1": {
  //     "ID1": {
  //       "cn": "Name",
  //       "objectGUID": "GUID"
  //     },
  //     "ID2": {
  //       "cn": "Name",
  //       "objectGUID": "GUID"
  //     }
  //   },
  //   "Name2": {
  //     "ID3": {
  //       "cn": "Name",
  //       "objectGUID": "GUID"
  //     }
  //   }
  // }
}