Q:

Update value in JSON object using NodeJS

I've got a JSON object that's being submitted to a AWS Lambda NodeJS function. This JSON object has an apostrophe in one of the fields that I need to escape before it's being inserted into a MySQL database.
The object needs to stay intact as it's being stored as a JSON object in the database. I've looked at string replace functions but those won't work since this is a JSON object natively.
I'm sure there is a simple answer here, I'm just very new to NodeJS and haven't found a way after searching around for a few hours. Thanks in advance!
The field I need to update is 2.1 below:
Example of the BAD JSON Object:
{
  "field1": "ABCD1234DEFG4567",
  "field2": "FBI",
  "fieldgroup": {
    "1.1": "ABCD",
    "1.2": 20170721,
    "1.3": "ABCD",
    "2.1": "L'astName, FirstName M"
  }
}

Example of the FINAL JSON object:
{
  "field1": "ABCD1234DEFG4567",
  "field2": "FBI",
  "fieldgroup": {
    "1.1": "ABCD",
    "1.2": 20170721,
    "1.3": "ABCD",
    "2.1": "L''astName, FirstName M"
  }
}

A:

const o = {
    "field1": "ABCD1234DEFG4567",
    "field2": "FBI",
    "fieldgroup": {
        "1.1": "ABCD",
        "1.2": 20170721,
        "1.3": "ABCD",
        "2.1": "L'astName, FirstName M"
      }      
   };
   
const preparedObject = prepare(o);
console.log(JSON.stringify(preparedObject, null, 4));

function prepare(o) {
  const replacedStrings = Object.keys(o)
    .filter(key => typeof o[key] === "string")
    .reduce((accu, key) => ({ ...accu, [key]: o[key].replace("'", "''") }), {});
  const preparedChildren = Object.keys(o)
    .filter(key => typeof o[key] === "object")
    .reduce((accu, key) => ({ ...accu, [key]: prepare(o[key]) }), {});
  return { ...o, ...replacedStrings, ...preparedChildren };
}

