Sample code for 30+ languages & platforms
React Native

JSON: Renaming and Deleting Members

See more JSON Examples

Demonstrates renaming and deleting members. This example uses the following JSON document:
{
   "apple": "red",
   "lime": "green",
   "banana": "yellow",
   "broccoli": "green",
   "strawberry": "red"
}

Chilkat React Native Downloads

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

function chilkatExample() {
  const json = new JsonObject();

  try {
    json.load('{"apple": "red","lime": "green","banana": "yellow","broccoli": "green","strawberry": "red"}');
  } catch {
    console.log(json.lastErrorText);
    return;
  }

  // Rename "lime" to "lemon".
  json.rename('lime', 'lemon');
  // Change the color to yellow:
  json.setStringOf('lemon', 'yellow');

  // Rename by index.  Banana is at index 2 (apple is at index 0)
  json.renameAt(2, 'bartlett_pear');

  // Delete broccoli by name
  json.deleteMember('broccoli');

  // Delete apple by index.  Apple is at index 0.
  json.deleteAt(0);

  json.emitCompact = false;
  console.log(json.emit());
}