I have an Array var cars = [2,3,..] which holds a few integers.
I've added a few values to the array, but I now need to send this array to a page via jQuery's .get method. How can I convert it to a JSON object for sending?
I have an Array var cars = [2,3,..] which holds a few integers.
I've added a few values to the array, but I now need to send this array to a page via jQuery's .get method. How can I convert it to a JSON object for sending?
Script for backward-compatibility: https://github.com/douglascrockford/JSON-js/blob/master/json2.js
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
One other way could be this:
var json_arr = {};
json_arr["name1"] = "value1";
json_arr["name2"] = "value2";
json_arr["name3"] = "value3";
var json_string = JSON.stringify(json_arr);
Or try defining the array as an object. (var cars = {};) Then there is no need to convert to json. This might not be practical in your example but worked well for me.
because my array was like below: and I used .push function to create it dynamically
my_array = ["234", "23423"];
The only way I converted my array into json is
json = Object.assign({}, my_array);
I think this is the best and cleanest answer.
Using
JSON.stringify(), we convert the JavaScript array to Json string. And usingJSON.parse(), we convert Json string to a Json object.
var JsonObject, jsArray;
// JS array
jsArray = ["one", "two", "three"];
// Convert (js array) to (json object)
JsonObject = JSON.parse(JSON.stringify(jsArray));
// type
console.log(typeof(JsonObject));
// JsonObject
console.log(JsonObject);
To generate valid json from array of integers use
let json = `[${cars}]`
for more general arrays use JSON.stringify(cars) (for object with circular references use this)
let cars = [1,2,3]; cars.push(4,5,6);
let json = `[${cars}]`;
console.log(json);
console.log(JSON.parse(json)); // json validation
You can convert an array to JSON using the Object.assign function. The main issue that I hit with that is that Javascript arrays have a zero based integer key, which in turn allows to access the data in the array.
let fruits = ['Apple', 'Banana']
document.write(fruits[0])
Returns:
Apple
When you convert the array to JSON, you get
{"0": "Apple", "1: "Banana"}
JSON objects on the contrary usually contain arbitrary keys, such as:
{ "some_key": "some value", "other_key: "some other value" }
Happily, you can create a list of items, which works pretty much as an array and then use Object.assign as if it was an array, then convert the output to a JSON object
var items = {};
items["some_key"]="some value";
items["other_key"]="some other value";
const jsonString = JSON.stringify(Object.assign({}, items))
const json_obj = JSON.parse(jsonString);
The advantage of working with item lists is that you don't have to deal with the JSON data as a string or quotes, commas etc... You can instead just manage the item object filling data in or removing it. When you are done you just convert the item variable to a JSON object and do whatever you need to do with it.
With your cars array: var cars = [2,3,..]. You can use for loop like this
var carArray = new Array();
for(var c in cars) {
var jsonObj = new Object();
jsonObj.car_id = cars[c];
carArray.push(jsonObj);
}