Home > Enterprise >  Transform variables in JSON format with JS/jQuery
Transform variables in JSON format with JS/jQuery

Time:01-28

I have these JS variables:

var var1 = 'Banana';
var var2 = 'Apple';
var var3 = 'Cherry';

How can I transform, them in JSON like this ?

{
    var1:"Banana",
    var2:"Apple",
    var3:"Cherry"
}

Thanks.

CodePudding user response:

JavaScript provides JSON.stringify for serialization in JSON and JSON.parse for reading from JSON.

const json = JSON.stringify({ var1, var2, var3 });
  • Related