Question: I have a scenario where many fields in JSON response that are coming as string (`"true"/"false"`).

Now I need to replace all of the values from `string` to `Boolean` in one shot via `Javascript`.

Sample:

```
{
    field1: "true",
    field2: "false"
}

```

Expected:

```
{
    field1: true,
    field2: false
}

```

This is a sample. My JSON response is very huge with many objects and arrays.

Answer: 
The [`JSON.parse` reviver parameter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Examples) can be used to exclude or modify values:

```js
var j = '{"field1":"true","field2":"false"}';

var o = JSON.parse(j, (k, v) => v === "true" ? true : v === "false" ? false : v);

console.log(o);
```
Here's a nice recursive `convertBools` function:

```js
function convertBools(obj) {
  const newObj = {};

  if (typeof obj !== 'object') {
    return obj;
  }

  for (const prop in obj) {
    if (!obj.hasOwnProperty(prop)) {
      continue;
    }
    if (Array.isArray(obj[prop])) {
      newObj[prop] = obj[prop].map(val => convertBools(val));
    } else if (obj[prop] === "true") {
      newObj[prop] = true;
    } else if (obj[prop] === "false") {
      newObj[prop] = false;
    } else {
      newObj[prop] = convertBools(obj[prop]);
    }
  }

  return newObj;
}

// Some examples below:
const obj1 = {
  field1: "true",
  field2: "false"
};

const obj2 = {
  field1: "true",
  field2: "asdf",
  field3: "false",
  field4: {
    field4a: "true",
    field4b: "asdf",
    field4c: ["a", {"array1": "true"}, "c"],
  }
}

const ret1 = convertBools(obj1);

console.log(ret1);

const ret2 = convertBools(obj2);

console.log(ret2);
```