React Native Requires Chilkat v11.0.0+
React Native
Swap JSON Objects
See more JSON Examples
Demonstrates how to swap two JSON objects within a JSON document.Chilkat React Native Downloads
import { JsonObject } from '@chilkat/react-native'
function chilkatExample() {
const json = new JsonObject();
json.emitCompact = false;
// Load the following JSON:
// {
// "petter": {
// "DOB": "26/02/1986",
// "gender": "male",
// "country": "US"
// },
// "Sara": {
// "DOB": "13/05/1982",
// "gender": "female",
// "country": "FR"
// },
// "Jon": {
// "DOB": "19/03/1984",
// "gender": "male",
// "country": "UK"
// }
// }
try {
json.loadFile('qa_data/json/people.json');
} catch {
console.log(json.lastErrorText);
return;
}
// Swap the positions of Jon and Sara.
let index1 = json.indexOf('Jon');
let index2 = json.indexOf('Sara');
json.swap(index1, index2);
// We have this now:
console.log(json.emit());
// {
// "petter": {
// "DOB": "26/02/1986",
// "gender": "male",
// "country": "US"
// },
// "Jon": {
// "DOB": "19/03/1984",
// "gender": "male",
// "country": "UK"
// },
// "Sara": {
// "DOB": "13/05/1982",
// "gender": "female",
// "country": "FR"
// }
// }
// To swap an inner member:
const jsonSara = new JsonObject();
json.objectOf2('Sara', jsonSara);
index1 = jsonSara.indexOf('DOB');
index2 = jsonSara.indexOf('country');
jsonSara.swap(index1, index2);
// We now have this:
console.log(json.emit());
// {
// "petter": {
// "DOB": "26/02/1986",
// "gender": "male",
// "country": "US"
// },
// "Jon": {
// "DOB": "19/03/1984",
// "gender": "male",
// "country": "UK"
// },
// "Sara": {
// "country": "FR",
// "gender": "female",
// "DOB": "13/05/1982"
// }
// }
}