Convert array to JSON Convert array to JSON json json

Convert array to JSON

Script for backward-compatibility:

And call:

var myJsonString = JSON.stringify(yourArray);

Note: The JSON object is now part of most modern web browsers (IE 8 & above). See caniuse for full listing. Credit goes to: @Spudley for his comment below

I made it that way:

if I have:

var jsonArg1 = new Object(); = 'calc this';    jsonArg1.value = 3.1415;var jsonArg2 = new Object(); = 'calc this again';    jsonArg2.value = 2.73;var pluginArrayArg = new Array();    pluginArrayArg.push(jsonArg1);    pluginArrayArg.push(jsonArg2);

to convert pluginArrayArg (which is pure javascript array) into JSON array:

var jsonArray = JSON.parse(JSON.stringify(pluginArrayArg))

Wow, seems it got a lot easier nowadays... 3 ways you can do it:

json = { ...array };json = Object.assign({}, array);json = array.reduce((json, value, key) => { json[key] = value; return json; }, {});