JavaScript arrays are designed to hold data with numeric indexes. You can add named properties to them because an array is a type of object (and this can be useful when you want to store metadata about an array which holds normal, ordered, numerically indexed data), but that isn't what they are designed for.
The JSON array data type cannot have named keys on an array.
When you pass a JavaScript array to JSON.stringify the named properties will be ignored.
If you want named properties, use an Object, not an Array.
const test = {}; // Object
test.a = 'test';
test.b = []; // Array
test.b.push('item');
test.b.push('item2');
test.b.push('item3');
test.b.item4 = "A value"; // Ignored by JSON.stringify
const json = JSON.stringify(test);
console.log(json);
Answer from Quentin on Stack OverflowVideos
JavaScript arrays are designed to hold data with numeric indexes. You can add named properties to them because an array is a type of object (and this can be useful when you want to store metadata about an array which holds normal, ordered, numerically indexed data), but that isn't what they are designed for.
The JSON array data type cannot have named keys on an array.
When you pass a JavaScript array to JSON.stringify the named properties will be ignored.
If you want named properties, use an Object, not an Array.
const test = {}; // Object
test.a = 'test';
test.b = []; // Array
test.b.push('item');
test.b.push('item2');
test.b.push('item3');
test.b.item4 = "A value"; // Ignored by JSON.stringify
const json = JSON.stringify(test);
console.log(json);
Nice explanation and example above. I found this (JSON.stringify() array bizarreness with Prototype.js) to complete the answer. Some sites implements its own toJSON with JSONFilters, so delete it.
if(window.Prototype) {
delete Object.prototype.toJSON;
delete Array.prototype.toJSON;
delete Hash.prototype.toJSON;
delete String.prototype.toJSON;
}
it works fine and the output of the test:
console.log(json);
Result:
"{"a":"test","b":["item","item2","item3"]}"
This is weird but I am using a library that adds additional properties to array objects.
We have a simple array of numbers that looks like this:
[1,2,3,4,5,6]
This library I'm using (developed in house unfortunately) gives us the array but also adds extra props to it. When I console.log the array, what I actually see is:
[1,2,3,4,5,6, low: 1, high: 6, median: 3]
Unfortunately, JSON.stringify causes this info to get lost:
'[1,2,3,4,5,6]'
To make things more difficult, I do not know the exact properties that are "attached" to the array (sometimes its mean, sometimes its something completely domain specific).
How can I deal with these weird data types at runtime such that this info can be sent over the wire? Is it even possible? I imagine it breaks JSON schema of some sort.
» npm install fast-json-stringify
Your object misses a comma as shown below:
name: "Blue Stilton",
age: "13"//comma is missing here
smelly: true
JSON.stringify works fine as shown below.
var cheese_array = [
{
name: "Chedder",
age: "34",
smelly: true
},
{
name: "Brie",
age: "4",
smelly: false
},
{
name: "Blue Stilton",
age: "13",
smelly: true
}
];
console.log(JSON.stringify(cheese_array))
However I am not sure how you get a log [object Object], [object Object], [object Object] I am presuming you are console logging something else please check that in your code.
Update JSON with ,(Comma) in
name: "Blue Stilton",
age: "13",
smelly: true
var cheese_array = [
{
name: "Chedder",
age: "34",
smelly: true
},
{
name: "Brie",
age: "4",
smelly: false
},
{
name: "Blue Stilton",
age: "13",
smelly: true
}
];
var details = JSON.stringify(cheese_array);
alert(details);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>