Twine Sugarcube .length not working

Please specify version and format if asking for help, or apply optional tags above:
Twine Version: 2
Story Format: Sugarcube

Hey there, I’m helping my brother with a Twine game and I’ve run into what is probably a rookie error with arrays/objects.
I’m defining a list of companions with an array of actions and I need to loop through these actions, however whenever I try to access the length variable it doesn’t exist (and just prints out the variable name). Here’s my code:

<<set $companions={

	"brother":{
		"hp":100,
		"name":"brother",
		"actions":{
			0:{
				"name":"punch"
			},
			1:{
				"name":"taunt"
			}
		}
	},
	"sister":{
		"hp":80,
		"name":"sister",
		"actions":{
			0:{
				"name":"kick"
			},
			1:{
				"name":"punch"
			}
		}
	}

}>>

$companions['brother']['actions'].length /* Prints out the variable string not the actual length of the actions array! */

<<set _companion="brother">>
$companions[_companion]['actions'].length /* Prints out the variable name rather than an int length */

<<for _i=0; _i lt $companions[_companion]['actions'].length; _i++>>
	Action is: $companions[_companion]['actions'][_i]['name']
<</for>>

Basically it never enters the for loop.
There seems to be some weirdness with multidimensional arrays in sugar cube, but even if I simplify it and just try to get the length of the array inside an array it returns nothing. Is there another way to get the length of an array in Sugarcube 2?

You have two problems here:

1. You apparently don’t know how to construct a JavaScript Array. Your code:

		"actions":{
			0:{
				"name":"punch"
			},
			1:{
				"name":"taunt"
			}
		}

Defines .actions as a generic object with numeric keys. What you needed to do was something like the following:

		"actions": [
			{
				"name":"punch"
			},
			{
				"name":"taunt"
			}
		]

Note the use of the array literal syntax ([]), rather than the object literal syntax ({}), and the absence of keys.

2. As noted in the naked variable markup documentation, it doesn’t support expressions as involved as $companions['brother']['actions'].length. You’ll need to use the <<print>> macro or its shortcut in this case. E.g.,

<<print $companions['brother']['actions'].length>>

<<= $companions['brother']['actions'].length>>