Sample code for 30+ languages & platforms
Dart

JSON Copy Objects

See more JSON Examples

Copy objects from one JSON document to another.

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  final json1 = CkJsonObject();
  json1.updateString('ID1.cn', 'Name');
  json1.updateString('ID1.objectGUID', 'GUID');
  json1.updateString('ID2.cn', 'Name');
  json1.updateString('ID2.objectGUID', 'GUID');

  json1.emitCompact = false;
  print(json1.emit());

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

  final json2 = CkJsonObject();
  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;
  print(json2.emit());

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

  // Copy Name1, Name2 into json1

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

  // Now see what json1 contains:
  print(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"
  //     }
  //   }
  // }
}