Is it possible to stringify a JSON object to look like this, with arrays in one line - not indented
{ "Repeat": { "Name": [["Top_level","All"],[[1,1]]], "Link": [["Top_level"],[[1,1]]] }, "Delete": ["Confirm","Cancel"], "Move": ["Up","Down"], "Number": ["Ascending","Descending"] }
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.
Stringify a JavaScript ArrayIt is also possible to stringify JavaScript arrays: Imagine we have this array in JavaScript: const arr = ["John", "Peter", "Sally", "Jane"]; Use the JavaScript function JSON.
structure your data: break the multiline string into an array of strings, and then join them later on. Try hjson tool. It will convert your multiline String in json to proper json-format.
stringify does not stringify nested arrays. Bookmark this question. Show activity on this post.
Try this:
var obj = {"Repeat": {"Name":[["Top_level","All"],[[1,1]]],"Link": [["Top_level"],[[1,1]]]},"Delete": ["Confirm","Cancel"],"Move": ["Up","Down"],"Number": ["Ascending","Descending"]}; JSON.stringify(obj,function(k,v){ if(v instanceof Array) return JSON.stringify(v); return v; },2);
Result:
"{ "Repeat": { "Name": "[[\"Top_level\",\"All\"],[[1,1]]]", "Link": "[[\"Top_level\"],[[1,1]]]" }, "Delete": "[\"Confirm\",\"Cancel\"]", "Move": "[\"Up\",\"Down\"]", "Number": "[\"Ascending\",\"Descending\"]" }"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With