0

JSON:

{  
   "time":"1520480018644",
   "start":"0",
   "search":{  
      "search":"",
      "smart":"true",
      "regex":"false",
      "caseInsensitive":"true"
   },
   "columns":[  
      {  
         "visible":"false",
         "search":{  
            "search":"",
            "smart":"true",
            "regex":"false",
            "caseInsensitive":"true"
         },
         "width":"200px"
      },
      {  
         "visible":"true",
         "search":{  
            "search":"",
            "smart":"true",
            "regex":"false",
            "caseInsensitive":"true"
         },
         "width":"200px"
      }
   ]
}

Program:

$.each(json_data, function(index, val) {
    $.each(val, function(index1, val1) {
        if(val1 == 'true'){
            json_data[index][index1] = True;
        }
        if(val1 == 'false'){
            json_data[index][index1] = False;
        }
    });
});

As you can see all boolean value in the string. How can I change the string to boolean respectively? I don't have any alternative way I just need to change the string to boolean respectively. Sorry for my weak English.

  • 3
    Possible duplicate of [How can I convert a string to boolean in JavaScript?](https://stackoverflow.com/questions/263965/how-can-i-convert-a-string-to-boolean-in-javascript) – 31piy Mar 08 '18 at 04:37
  • I added answer into the post . I hope it will work as per your expectation. – Debug Diva Mar 08 '18 at 04:54

3 Answers3

0

Create a simple toBoolean method

function toBoolean( str )
{
   return str == "true";
}

And use it as

$.each(json_data, function(index, val) {
    $.each(val, function(index1, val1) {
        json_data[index][index1] = toBoolean( val1 );
    });
});
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
0

Made a version that should go through the tree and change all the nested ones.

var json_data = {
  "time": "1520480018644",
  "start": "0",
  "search": {
    "search": "",
    "smart": "true",
    "regex": "false",
    "caseInsensitive": "true"
  },
  "columns": [{
      "visible": "false",
      "search": {
        "search": "",
        "smart": "true",
        "regex": "false",
        "caseInsensitive": "true"
      },
      "width": "200px"
    },
    {
      "visible": "true",
      "search": {
        "search": "",
        "smart": "true",
        "regex": "false",
        "caseInsensitive": "true"
      },
      "width": "200px"
    }
  ]
};

function processObject(object) {
  //loop over the object key values or the array elements
  $.each(object, function(key, value) {
    //if the value is a string, we want to possibly convert it
    if (typeof value === 'string') {
      //only convert if the value is true/false
      if (['true', 'false'].indexOf(value.toLowerCase()) > -1) {
        object[key] = (value.toLowerCase() == 'true');
      }
    } else {
      //the value is not a string, try to process it as an object or array
      processObject(value);
    }
  });
}

processObject(json_data);

console.log(json_data);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Taplar
  • 24,788
  • 4
  • 22
  • 35
0

Try this working demo :

var jsonObj = {
 "time": "1520480018644",
 "start": "0",
 "search": {
  "search": "",
  "smart": "true",
  "regex": "false",
  "caseInsensitive": "true"
 },
 "columns": [{
   "visible": "false",
   "search": {
    "search": "",
    "smart": "true",
    "regex": "false",
    "caseInsensitive": "true"
   },
   "width": "200px"
  },
  {
   "visible": "true",
   "search": {
    "search": "",
    "smart": "true",
    "regex": "false",
    "caseInsensitive": "true"
   },
   "width": "200px"
  }
 ]
};


function convertBool(obj) {
  if (typeof obj !== 'object') {
    return;
  }
  
  for (var i in Object.keys(obj)) {
     (obj[Object.keys(obj)[i]] == "true") ? obj[Object.keys(obj)[i]] = true : convertBool(obj[Object.keys(obj)[i]]);
     (obj[Object.keys(obj)[i]] == "false") ? obj[Object.keys(obj)[i]] = false : convertBool(obj[Object.keys(obj)[i]]);
  }
  return jsonObj;
}

var convertedObj = convertBool(jsonObj);

console.log(convertedObj);
Debug Diva
  • 26,058
  • 13
  • 70
  • 123