I have an array in JavaScript that looks like this:
var pattern = [ ["C5", 3], , , , , , , , ["C5", 3], , , ]
I want to store it in a json file like this:
{
"pattern": [
["C5", 3], , , , , , , , ["C5", 3], , ,
]
}
JSONLint tells me this:
Parse error on line 6:
... ], , ,
---------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['
So I understand I can't let the space between mas empty. What's similar to empty, but is accepted by JSON standards?
This pattern file is part of a Javascript Music Tracker I'm making that's similar to impulse tracker, and I want the json file to be as clean as possible.
I have an array in JavaScript that looks like this:
var pattern = [ ["C5", 3], , , , , , , , ["C5", 3], , , ]
I want to store it in a json file like this:
{
"pattern": [
["C5", 3], , , , , , , , ["C5", 3], , ,
]
}
JSONLint tells me this:
Parse error on line 6:
... ], , ,
---------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['
So I understand I can't let the space between mas empty. What's similar to empty, but is accepted by JSON standards?
This pattern file is part of a Javascript Music Tracker I'm making that's similar to impulse tracker, and I want the json file to be as clean as possible.
Share Improve this question edited Oct 31, 2015 at 0:46 Supersharp 31.3k11 gold badges102 silver badges147 bronze badges asked Jun 2, 2015 at 0:38 eri0oeri0o 2,4504 gold badges31 silver badges43 bronze badges 5-
5
null
is the "empty" value in JSON. – Jonathan Lonowski Commented Jun 2, 2015 at 0:41 -
1
fill empty places with
''
empty string – vsync Commented Jun 2, 2015 at 9:59 - Empty string looks like a nice approach. Will try. – eri0o Commented Jun 2, 2015 at 10:02
-
1
fill empty places with
0
number. That will take one character instead of 2 for empty string.0 == null == ''
– Supersharp Commented Oct 29, 2015 at 13:33 -
1
I ended using
[]
at the time for simplicity. It worked and I haven't touched since... github./ericoporto/fangamk/tree/master/GameMusic – eri0o Commented Oct 29, 2015 at 14:31
2 Answers
Reset to default 6If you want to have empty space in JSON, you should fill it with null
.
Example:
var pattern = [ ["C5", 3], null, null, null, null, null, null, null, ["C5", 3], null, null, null, null, ...... ]
If you don't want to use null
with a sparse array, you could use the object representation of an array.
pattern = [["C5", 3], , , , , , , , ["C5", 3], , , ]
... would be replaced by an object with index value as properties and an additional length
property:
parse_array = {
0: ["C5", 3],
8: ["C5", 3],
length: 12
}
This object can be generated from the array using its reduce
method:
parse_array = pattern.reduce( ( obj, val, i ) =>
{
obj.length++
if ( val )
obj[i] = val
return obj
}, { length: 0 } )
Then it's easy to convert the object into a JavaScript Array, using a simple arrow function:
pattern = Array.from( parse_array, v => v )
//Encode array
var pattern = [["C5",3],null,null,null,null,null,null,null,["C5",3],null,null,null]
out.innerHTML = JSON.stringify( pattern ) + "\n"
var parse1 = pattern.reduce( ( obj, val, i ) =>
{
obj.length++
if ( val )
obj[i] = val
return obj
}, { length } )
out.innerHTML += JSON.stringify( parse1 ) + "\n"
//Decode object
var parse2 = {
0: ["C5", 3],
8: ["C5", 3],
length: 12
}
out.innerHTML += JSON.stringify( parse2 ) + "\n"
out.innerHTML += JSON.stringify( Array.from( parse2, v => v ) ) + "\n"
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<h3>JSON array with empty items</h3>
<pre id="out"></pre>
</body>
</html>