How to convert boolean and number type Json Key value to string type

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


How to convert boolean and number type Json Key value to string type



Iam having a json object where it has different key values of string,boolean and number type.I want to convert the boolean and number type key value to string type..i.e.,eg{"rent":2000,"isPaid":false} which is a valid json .Here i want to convert rent and isPaid to string type i.e.,{"rent":"2000","isPaid":"false"} which is also valid.for this Iam using replacer,but not working exactly how i require


var json={"rent":2000,"isPaid":false};
var jsonString = JSON.stringify(json, replacer);
function replacer(key, value) {
if (typeof value === "boolean"||typeof value === "number") {
return "value";
}
return value;
}
console.log(jsonString);



Then above code is consoling as {"rent":"value","isPaid":"value"}



Then I replaced return "value" with return '"'+value+'"' .Then on console it is giving {"rent":""2000"","isPaid":""false""}



So can someone help me so that it returns as {"rent":"2000","isPaid":"false"}..



Any help is apreciable..Thanks!




4 Answers
4



Try this:


var json={"rent":2000,"isPaid":false};
var jsonString = JSON.stringify(json, replacer);

function replacer(key, value) {
if (typeof value === "boolean"||typeof value === "number") {
return String(value);
}
return value;
}

console.log(jsonString);



We're using the String() function to convert your booleans and numbers to string. The output is:


String()


{"rent":"2000","isPaid":"false"}





Thanks this worked out.
– H Varma
Aug 26 '16 at 6:38





@HarishVarma Glad to help! Don't forget to up-vote.
– yas777
Aug 27 '16 at 17:24



You can do it by this way...


var json={"rent":2000,"isPaid":false};
var jsonString = JSON.stringify(json, replacer);
function replacer(key, value) {
if (typeof value === "boolean"||typeof value === "number") {
return value=""+value+"";
}
return value;
}
console.log(jsonString);



You can use JavaScript's toString() method to convert numbers/boolean to change them to string, as below:


toString()


var json={"rent":2000,"isPaid":false};
var temp = {};
json.forEach(function(val, key)
{
temp[key] = val.toString();
});
json = temp;



I do it like this with less code in es6 style, using JSON.parse(text[, reviver]) "reviver" parameter.


JSON.parse(text[, reviver])



https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse


let reviver = (key, value) => (typeof value === 'number' || typeof value === 'boolean') ? String(value) : value;






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

C# - How to create a semi transparent or blurred backcolor on windows form

Will Oldham

Makefile test if variable is not empty