Javascript Json Parse and convert boolean value

Description

Javascript Json Parse and convert boolean value

function convertBoolToNums(key, value) {
  if (typeof value == 'boolean') {
    if (value)//from  ww w  .j a va 2s.com
      value = 1;
    else
      value = 0;
  }
  return value;
};

let obj = {
  "test" : "value1",
  "test2" : 3.44,
  "test3" : false
};
let jsonobj = JSON.stringify(obj, convertBoolToNums, 3);

console.log(jsonobj); // test3 : 0



PreviousNext

Related