7

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 commas 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.

5
  • 5
    null is the "empty" value in JSON. Commented Jun 2, 2015 at 0:41
  • 1
    fill empty places with '' empty string Commented Jun 2, 2015 at 9:59
  • Empty string looks like a nice approach. Will try. 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 == '' 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.com/ericoporto/fangamk/tree/master/GameMusic Commented Oct 29, 2015 at 14:31

2 Answers 2

6

If 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, ...... ]
Sign up to request clarification or add additional context in comments.

1 Comment

I don't get why my question is getting downvoted to hell but I understand your answer. I knew null but I was wondering if there was something cleaner - 4 characters can add a lot in bigger songs with multiple channels. Thanks, will think about it.
4

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.