下面是一个严格规则的 n 维数组的通用解决方案:
.data | [recurse( .[0]? | select(type=="array")) | length]
有关严格规律性概念的更多信息,请参见下文。
作为一个函数
有
def rho: [recurse( .[0]? | select(type=="array")) | length];
[ [[1,1],[2,2]], [[1,1],[2,2]], [[1,1],[2,2]]] | rho
将产生:[3,2,2]
严格规律
为了目前的目的,假设一个“矩阵”是严格规则的,如果它的条目都不是数组,并且
当且仅当对应的 n 维矩阵是严格规则时,n 维 JSON 数组才是严格规则的。
为了检查一个 JSON 数组在这个意义上是严格正则的,我们可以定义is_strictly_regular(使用上面的rho 的定义)如下:
# Check whether the input has precisely the specified
# dimensions, and no additional arrays.
# It is assumed that $dimensions is an array of non-negative integers.
# If $dimensions is [], then return type!="array".
def strictcheck($dimensions):
if type == "array"
then ($dimensions|length) > 0
and length == $dimensions[0]
and all(.[]; strictcheck($dimensions[1:]))
else $dimensions == []
end;
def is_strictly_regular:
rho as $d
| strictcheck($d);